Search code examples
phpjsoncurlwunderlist

cURL returns half json string in most cases


First edit (more details):

  • I used Modify Headers plugin in Firefox and visited the page (http://api.wunderlist.com/me/tasks), this showed the full and correct json string that I need. It never fails (25+ tests)
  • I tried using file_get_contents, but also file_get_contents cuts the response down. At various points.
  • cURL does the same, cuts down the response json at various points. Looks "at random"

Original post:

I'm breaking my head on a strange issue here. I've created a PHP Wrapper for Wunderlist2 (http://www.wunderlist.com) which can be found here: https://github.com/PENDOnl/Wunderlist2-PHP-Wrapper

It worked perfectly until I was notified by a user that the class stopped working all of a sudden. Since I created a free service (http://wcal.me) to provide a calendar feed for Wunderlist users, I decided to take a look at that script since that's the most easy way for me to debug the script.

Login in to Wunderlist and fetching the authtoken works, also all other functions in the class seem to work find (getting 'me', 'me/lists', etc.) However, in case of the getTasks function ('me/tasks') the response that I get is not complete, it simply stops in the middle of a json string. Therefor the json_decode function returns NULL and thus no tasks will be available in the calender feed/method response.

I also noticed that it's pretty random, because in some cases (<10%) it works like it should, but after another refresh the output is cut in half again. Also, the exact location of the 'cut' is different per refresh.

Is there anyone that can identify what the problem is? I tried to see if there is a way to wait untill a complete download of the file before it is returned, but it would be strange if that wasn't the default behaviour of cURL. I also tried to increase the timeout time, but since it returns a value I guess it doesn't timeout either.

All code can be foundin the Github repo, so far this is the only part I've changed to debug:

        // get / put / delete requests should have HTTP Code 200 OK
        // only exception is the login method, which returns HTTP Code 200 OK
        if($httpCode == 200 && (strtolower($method) != 'post' || $action == '/login'))
        {
            $return = json_decode($output, true);

            if($_SERVER['REMOTE_ADDR'] == MY_IP) {
                if( is_null($return) ) {
                    echo "<b>Output of json_decode is null:</b><br><br>";
                    echo $output;   
                } else {
                    echo "<b>Output of json_decode os not null:</b><br><br>";
                    echo $output;   
                }
            }

            return $return;
        }

The full response should look like something similar to this:

[{"assignee_id":null,"completed_at":null,"completed_by_id":null,"created_at":"2013-11-10T13:16:54Z"},{"assignee_id":null,"completed_at":null,"completed_by_id":null,"created_at":"2013-11-10T13:16:54Z"}]

But instead in most cases it's:

[{"assignee_id":null,"completed_at":null,"completed_by_id":null,"created_at":"2013-11-10T13:16:54Z"},{"assignee

Of course, this is a minified response, there is more information in the response available and there are many more items in the array.


Solution

  • Problem was with Wunderlist, just waiting for their fix :)