How to decode this json in php?

1

I saw some similar questions here in satckoverflow, but I did not find an answer to this problem.

I have a Result that brings me the following json:

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 07 Jul 2017 19:44:04 GMT
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
callId: 12476bea-0ebc-409d-86cc-69aac003e356
Strict-Transport-Security: max-age=63072000
X-Frame-Options: DENY
X-Content-Type-Options: nosniff

{"paginacao":{"pagina":1,"quantidadeRegistros":10,"quantidadeTotalRegistros":10}

I have a json_decode ($ result, true) element that can not separate the header, so it does not return the object as needed, which would only be from {"page ...

How to proceed in this case, I'm using curl_setopt () to consume json at source

    
asked by anonymous 07.07.2017 / 21:54

1 answer

5

Just remove the CURLOPT_HEADER option that returns the headers:

curl_setopt($ch, CURLOPT_HEADER, 1);

Or set 0

curl_setopt($ch, CURLOPT_HEADER, 0);

Then get the answer and use json_decode , for example:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

//Define um User-agent
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);

//Retorna a resposta
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//Resposta
$data = curl_exec($ch);

//depurando
var_dump($data);

var_dump(json_decode($data));

Read more in the documentation: link

    
07.07.2017 / 21:59