0

Json Response and Error Handling

Hi Everybody,
I'm facing this issue: I want to get the HTTP Headers to extract the STATUS of the query response (Success, Bad Requests, ectr.) see the link for the codes I found: YQL Error Codes.
I'm using Curl from php to obtain a Json response:
CODE
$yql_query_url = BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";
$ch = curl_init($yql_query_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); //Returns the headers

$json = curl_exec($ch); //RESULT of YQL QUERY

$status_code = array();
preg_match('/\d\d\d/', $json, $status_code);

switch( $status_code[0] ) {
case 200:
echo "HTTP status of 200: Success!";
// Success
break;
case 503:
die('Your call to Yahoo Web Services failed and returned an HTTP status of 503. That means: Service unavailable. An internal problem
prevented us from returning data to you.');
break;
case 403:
die('Your call to Yahoo Web Services failed and returned an HTTP status of 403. That means: Forbidden. You do not have permission to access this resource, or are over your rate limit.');
break;
case 400:
die('Your call to Yahoo Web Services failed and returned an HTTP status of 400. That means: Bad request. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
break;
case 401:
die('Your call to Yahoo Web Services failed and returned an HTTP status of 401. That means: Authorization Required. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
break;

case 999:
die('Your call to Yahoo Web Services failed and returned an HTTP status of 999. That means: Unable to process this request at this time. The parameters passed to the service did not match as expected. The exact error is returned in the XML response.');
break;
default:
die('Your call to Yahoo Web Services returned an unexpected HTTP status of:' . $status_code[0]);
}

$result=json_decode($json,true); //IT DOES NOT WORK

For the error handling I based my script on this example Php YQL Rest Error Handling

The problem is that the function json_decode does not work because the response is not a pure json string but it is mixed with the headers.
My Idea was to cut out the headers from the string, or either extract the json from the response string..is that right?
And how to do it?

Thanks, bye!

by
2 Replies
  • Hi,
    it's a PHP issue here I guess. You can grab the response code like that :
    CODE
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);


    There is a lot of options for this function and so you can set CURLOPT_HEADER to FALSE (this way json_decode will work like a charm).

    Damien.
    0
  • QUOTE (Damien @ Nov 26 2010, 08:34 AM) <{POST_SNAPBACK}>
    Hi,
    it's a PHP issue here I guess. You can grab the response code like that :
    CODE
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);


    There is a lot of options for this function and so you can set CURLOPT_HEADER to FALSE (this way json_decode will work like a charm).

    Damien.


    Thanks Damien, I've just solved extracting the json from the overall response using a regular expression:
    CODE
    preg_match('/{.*}/',$response, $json);

    the $json[0] variable will contain the extracted json. It can then be used with the function json_decode

    Thanks, bye
    0

Recent Posts

in YQL