guzzle and stackoverflow api

141 views
Skip to first unread message

Gagan Janjua

unread,
Jun 29, 2014, 11:25:32 PM6/29/14
to guz...@googlegroups.com

I'm trying to consume the Stack Exchange API with Guzzle. I am facing an issue where I can't get the JSON response back, it apparently fails when parsing it.

Here is my code :

$client = new GuzzleHttp\Client();
$parameters = ['pagesize'=>'2','order'=>'desc','sort'=> 'activity','q'=>'laravel eloquent','site'=>'stackoverflow'];
$response = $client->get('http://api.stackexchange.com/2.2/search/advanced',['query' => $parameters ]);

The resultant effective URL that Guzzle creates is correct, if you open the link in your browser you'll see that it works fine and returns the requested data.

However, Guzzle fails with this error when trying to access the JSON with $response->json() :

GuzzleHttp \ Exception \ ParseException Unable to parse JSON data: JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded

Update

After reading the documentation again, I believe that the request is compressed while I am not passing the appropriate the content-header . If it is so , can you please let me know which is the correct header I should be passing to get the correct response ?

Update 2

So I added the following two lines of code but its still failing

$response->addHeader('Accept-Encoding','GZIP');
$response->addHeader('Content-Type','application/json');

because the API is returning a JSON object


Reply all
Reply to author
Forward
0 new messages