Skip to content

Instantly share code, notes, and snippets.

@andreasciamanna
Created June 19, 2014 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasciamanna/3bde445afe80a0e8aae6 to your computer and use it in GitHub Desktop.
Save andreasciamanna/3bde445afe80a0e8aae6 to your computer and use it in GitHub Desktop.
Example of how we handle API responses
<?php
/*
Example of a json response object:
{
"response": { //Whatever is expected to be returned
"id": 11,
"firstName": "Woody",
"lastName": "Allen",
"email" : "wa@example.org"
},
"status": {
"code": 0,
"messagge": "some message, or empty if not needed"
}
*/
// Our PHP code handle standard API response as such:
protected static function _get_api_response( $response ) {
//No response and no status code
if ( !$response || !isset( $response->status->code ) ) {
throw new TranslationProxy_Api_Error( "Cannot communicate with the remote service" );
}
//Status code is not 0 (0 = OK): throw an error and provide among with the status code
if ( $response->status->code != 0 ) {
throw new TranslationProxy_Api_Error( $response->status->message, $response->status->code );
}
//Everything is fine, return just the response part
return $response->response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment