Gabriel Ribeiro
Gabriel Ribeiro

Reputation: 13

Why when trying to consume a json with curl in php, I get a null result?

It is a simple json containing states of a country, when accessing directly from the browser works normally. But when trying to consume by curl, the result is null. Here's my code:

$ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , false);
            curl_setopt($ch, CURLOPT_URL, "http://servicodados.ibge.gov.br/api/v1/localidades/estados");
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8") );
            $result = curl_exec($ch);

            curl_close($ch);

            $estados = json_decode($result, true);

            print_r($estados);
            die();

Upvotes: 0

Views: 35

Answers (1)

Vladan
Vladan

Reputation: 1632

It seems that the response is binary, you can add this option as well to get the expected result.

curl_setopt($ch,CURLOPT_ENCODING , "");

Upvotes: 1

Related Questions