Scenario: REST api where a client requests data from server via GET method
I am returning an array from HomeController (Server side: Laravel 5)
return ['Status' => 'Success', 'SearchResponse' => $apiresponse, 'AuthToken' => $property];
The above response is generated from a URL http://example.com/flightSearch
On the client side (Laravel 4)
$input=Input::all();
$url = 'http://example.com/flightSearch';
$data = array(
'client_id' => 'XXX',
'api_secret' => 'YYY',
'method'=>'SearchFlight',
'adult'=>$input['adult'],
'children'=>$input['children'],
'infant'=>$input['infant'],
'departCity'=>$input['departCity'],
'arrivalCity'=>$input['arrivalCity'],
'departDate'=>$input['departDate'],
'returnDate'=>$input['returnDate'],
'journeyType'=>$input['journeyType']
);
$params = http_build_query($data);
$result = file_get_contents($url.'?'.$params);
$response = json_decode($result);
return $response->Status //Works
return $response->AuthToken //Works
return $response->SearchResponse //Throws following Error
Error:
The Response content must be a string or object implementing __toString()
Solution:
The variable $apiresponse was an object returned from a remote server. Adding the variable to an object solved the problem
return ['Status' => 'Success', 'SearchResponse' => array($apiresponse), 'AuthToken' => $property];
var_dump($response)and check what is the type of your variable?$response? and what are you doing to get/fetch the data?