Tiega Tiega - 1 month ago 9
reST (reStructuredText) Question

ZF2 RESTful JsonModel

I use PHP-cURL(in a WP-Plugin) to get a JSON response from my ZF2 application.

I have to do 2 requests.

  1. OAuth

  2. GET request

The first one works fine i just get the json_encoded array back.
But when i send the GET request the response looks like(everything local):

plugin.php:16:string 'HTTP/1.1 200 OK Date: Fri, 18 Nov 2016 13:14:20
GMT Server: Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9
X-Powered-By: PHP/7.0.9 Set-Cookie:
PHPSESSID=gb99a214u3rlc125ca4rscd441; expires=Fri, 18-Nov-2016
23:14:20 GMT; Max-Age=36000; path=/ Expires: Thu, 19 Nov 1981 08:52:00
GMT Cache-Control: no-store, no-cache, must-revalidate Pragma:
no-cache Transfer-Encoding: chunked Content-Type: application/json;

{"data":{"Machines":[{"ID":2590978,"Refnummer":1000869,"Maschinentyp":"504"'... (length=41759)

Why do i get the full response header?
If i now try to json_decode it i have to get rid of all the header stuff beforehand, can i avoid that?

This is how i generate the response

$server = call_user_func($this->oauthServerFactory, $this->params('oauth'));
if (!$server->verifyResourceRequest(OAuth2Request::createFromGlobals())) {
// Not authorized return 401 error
return $this->getResponse();


$jsonArray = array();
$jsonArray['Machines'] = array();
$jsonMachinesArray = array();
foreach ($machines as $machine)

return new JsonModel(array(
'data' => $jsonArray

And here i create the request:

$endpoint = $url;
$headers = array(
'Content-Type: application/json',
'Authorization: Bearer '.$access_token,
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

echo "Performing Request...<br>";

$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);

// evaluate for success response
if ($status != 200) {
throw new Exception("Error: call to URL $endpoint failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl) . "\n");

return $json_response;


It seems like this relates to the curl part in your client implementation. Please have a look at PHP cURL retrieving response headers AND body in a single request?.

I think it should be done with setting the line with CURLOPT_HEADER to false. (https://curl.haxx.se/libcurl/c/CURLOPT_HEADER.html)