Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

marcellopato's avatar

Guzzle API response

Hello folks! Greetings from Brazil! I made a GET request from my model to an API endpoint like:

class CNPJ extends Model
{
    static function getCNPJ($cnpj)
    {
        $client = new Client([
            'base_uri' => 'https://www.receitaws.com.br/v1/cnpj/'
        ]);
        $response = $client->request('GET', $cnpj);
        $dadosReceita = json_decode($response->getBody()->getContents());
        return $dadosReceita;
    }
}

My controller gets the result like:

$dadosReceita = CNPJ::getCNPJ($cnpj);
            $receita =  response()->json(($dadosReceita));
            return $receita;

And the result is:


{
"atividade_principal": [
{
"text": "Formação de condutores",
"code": "85.99-6-01"
}
],
"data_situacao": "25/02/2001",
"nome": "AUTO ESCOLA GUILHERMITTI & LOBANCO LTDA",
"uf": "SP",
"telefone": "(17) 3258-1649 / (17) 3258-1649",
"email": "[email protected]",
"qsa": [
{
"qual": "49-Sócio-Administrador",
"nome": "ELIANA CRISTINA GUILHERMITTI RODRIGUES"
},
{
"qual": "49-Sócio-Administrador",
"nome": "SUZANA MARQUES LOBANCO"
},
{
"qual": "49-Sócio-Administrador",
"nome": "AMARILDO APARECIDO RODRIGUES"
},
{
"qual": "22-Sócio",
"nome": "VALDEMIR FRANCISCO DA COSTA"
},
{
"qual": "49-Sócio-Administrador",
"nome": "ANDRE LUIS LOBANCO"
}
],
"situacao": "ATIVA",
"bairro": "CENTRO",
"logradouro": "R CASTRO ALVES",
"numero": "1408",
"cep": "15.115-000",
"municipio": "BADY BASSITT",
"porte": "MICRO EMPRESA",
"abertura": "02/06/1995",
"natureza_juridica": "206-2 - Sociedade Empresária Limitada",
"cnpj": "00.635.344/0001-77",
"ultima_atualizacao": "2019-01-03T10:43:40.334Z",
"status": "OK",
"tipo": "MATRIZ",
"fantasia": "",
"complemento": "",
"efr": "",
"motivo_situacao": "",
"situacao_especial": "",
"data_situacao_especial": "",
"atividades_secundarias": [
{
"code": "00.00-0-00",
"text": "Não informada"
}
],
"capital_social": "1000.00",
"extra": {},
"billing": {
"free": true,
"database": true
}
}

I know it looks pretty easy and basic, but I can´t get 'natureza_juridica', for example. Please, can anyone help?

I must record each ´key´ to my database.

Thanks in advance

0 likes
10 replies
JohnBraun's avatar
  • PHP: Did you try to get it using:
$receita->natureza_juridica;
  • Or if you'd like to accept it in Vue:
export default {

data() {
    return {
        receita = {};
    }
},

created() {
    axios.get('https://www.receitaws.com.br/v1/cnpj/').then((response) => {
        this.receita = response.data;   
    });
},
}

Then you could use this.receita.natureza_juridica to access the value

marcellopato's avatar

@JOHNBRAUN - In Vue there is a CORS issue. And Can´t ask the site´s owner to help me with that.

In PHP the error is:

Undefined property: Illuminate\Http\JsonResponse::$natureza_juridica

...as I already have tried.

I saw it working just fine on Laravel 5.4 and I am using 5.7. Weird hun?

JohnBraun's avatar

@MARCELLOPATO - What does a var_dump($receita) give you? Could you check if you can get it’s values using $receita->all() ?

marcellopato's avatar

@JOHNBRAUN - var_dump gives me:

object(Illuminate\Http\JsonResponse)#820 (11) { ["data":protected]=> string(1248) "{"atividade_principal":[{"text":"Forma\u00e7\u00e3o de condutores","code":"85.99-6-01"}],"data_situacao":"25\/02\/2001","nome":"AUTO ESCOLA GUILHERMITTI & LOBANCO LTDA","uf":"SP","telefone":"(17) 3258-1649 \/ (17) 3258-1649","email":"[email protected]","qsa":[{"qual":"49-S\u00f3cio-Administrador","nome":"ELIANA CRISTINA GUILHERMITTI RODRIGUES"},{"qual":"49-S\u00f3cio-Administrador","nome":"SUZANA MARQUES LOBANCO"},{"qual":"49-S\u00f3cio-Administrador","nome":"AMARILDO APARECIDO RODRIGUES"},{"qual":"22-S\u00f3cio","nome":"VALDEMIR FRANCISCO DA COSTA"},{"qual":"49-S\u00f3cio-Administrador","nome":"ANDRE LUIS LOBANCO"}],"situacao":"ATIVA","bairro":"CENTRO","logradouro":"R CASTRO ALVES","numero":"1408","cep":"15.115-000","municipio":"BADY BASSITT","porte":"MICRO EMPRESA","abertura":"02\/06\/1995","natureza_juridica":"206-2 - Sociedade Empres\u00e1ria Limitada","cnpj":"00.635.344\/0001-77","ultima_atualizacao":"2019-01-03T10:43:40.334Z","status":"OK","tipo":"MATRIZ","fantasia":"","complemento":"","efr":"","motivo_situacao":"","situacao_especial":"","data_situacao_especial":"","atividades_secundarias":[{"code":"00.00-0-00","text":"N\u00e3o informada"}],"capital_social":"1000.00","extra":{},"billing":{"free":true,"database":true}}" ["callback":protected]=> NULL ["encodingOptions":protected]=> int(0) ["headers"]=> object(Symfony\Component\HttpFoundation\ResponseHeaderBag)#819 (5) { ["computedCacheControl":protected]=> array(2) { ["no-cache"]=> bool(true) ["private"]=> bool(true) } ["cookies":protected]=> array(0) { } ["headerNames":protected]=> array(3) { ["cache-control"]=> string(13) "Cache-Control" ["date"]=> string(4) "Date" ["content-type"]=> string(12) "Content-Type" } ["headers":protected]=> array(3) { ["cache-control"]=> array(1) { [0]=> string(17) "no-cache, private" } ["date"]=> array(1) { [0]=> string(29) "Thu, 13 Jun 2019 16:24:27 GMT" } ["content-type"]=> array(1) { [0]=> string(16) "application/json" } } ["cacheControl":protected]=> array(0) { } } ["content":protected]=> string(1248) "{"atividade_principal":[{"text":"Forma\u00e7\u00e3o de condutores","code":"85.99-6-01"}],"data_situacao":"25\/02\/2001","nome":"AUTO ESCOLA GUILHERMITTI & LOBANCO LTDA","uf":"SP","telefone":"(17) 3258-1649 \/ (17) 3258-1649","email":"[email protected]","qsa":[{"qual":"49-S\u00f3cio-Administrador","nome":"ELIANA CRISTINA GUILHERMITTI RODRIGUES"},{"qual":"49-S\u00f3cio-Administrador","nome":"SUZANA MARQUES LOBANCO"},{"qual":"49-S\u00f3cio-Administrador","nome":"AMARILDO APARECIDO RODRIGUES"},{"qual":"22-S\u00f3cio","nome":"VALDEMIR FRANCISCO DA COSTA"},{"qual":"49-S\u00f3cio-Administrador","nome":"ANDRE LUIS LOBANCO"}],"situacao":"ATIVA","bairro":"CENTRO","logradouro":"R CASTRO ALVES","numero":"1408","cep":"15.115-000","municipio":"BADY BASSITT","porte":"MICRO EMPRESA","abertura":"02\/06\/1995","natureza_juridica":"206-2 - Sociedade Empres\u00e1ria Limitada","cnpj":"00.635.344\/0001-77","ultima_atualizacao":"2019-01-03T10:43:40.334Z","status":"OK","tipo":"MATRIZ","fantasia":"","complemento":"","efr":"","motivo_situacao":"","situacao_especial":"","data_situacao_especial":"","atividades_secundarias":[{"code":"00.00-0-00","text":"N\u00e3o informada"}],"capital_social":"1000.00","extra":{},"billing":{"free":true,"database":true}}" ["version":protected]=> string(3) "1.0" ["statusCode":protected]=> int(200) ["statusText":protected]=> string(2) "OK" ["charset":protected]=> NULL ["original"]=> object(stdClass)#792 (30) { ["atividade_principal"]=> array(1) { [0]=> object(stdClass)#790 (2) { ["text"]=> string(24) "Formação de condutores" ["code"]=> string(10) "85.99-6-01" } } ["data_situacao"]=> string(10) "25/02/2001" ["nome"]=> string(39) "AUTO ESCOLA GUILHERMITTI & LOBANCO LTDA" ["uf"]=> string(2) "SP" ["telefone"]=> string(31) "(17) 3258-1649 / (17) 3258-1649" ["email"]=> string(22) "[email protected]" ["qsa"]=> array(5) { [0]=> object(stdClass)#806 (2) { ["qual"]=> string(23) "49-Sócio-Administrador" ["nome"]=> string(38) "ELIANA CRISTINA GUILHERMITTI RODRIGUES" } [1]=> object(stdClass)#796 (2) { ["qual"]=> string(23) "49-Sócio-Administrador" ["nome"]=> string(22) "SUZANA MARQUES LOBANCO" } [2]=> object(stdClass)#802 (2) { ["qual"]=> string(23) "49-Sócio-Administrador" ["nome"]=> string(28) "AMARILDO APARECIDO RODRIGUES" } [3]=> object(stdClass)#803 (2) { ["qual"]=> string(9) "22-Sócio" ["nome"]=> string(27) "VALDEMIR FRANCISCO DA COSTA" } [4]=> object(stdClass)#805 (2) { ["qual"]=> string(23) "49-Sócio-Administrador" ["nome"]=> string(18) "ANDRE LUIS LOBANCO" } } ["situacao"]=> string(5) "ATIVA" ["bairro"]=> string(6) "CENTRO" ["logradouro"]=> string(14) "R CASTRO ALVES" ["numero"]=> string(4) "1408" ["cep"]=> string(10) "15.115-000" ["municipio"]=> string(12) "BADY BASSITT" ["porte"]=> string(13) "MICRO EMPRESA" ["abertura"]=> string(10) "02/06/1995" ["natureza_juridica"]=> string(38) "206-2 - Sociedade Empresária Limitada" ["cnpj"]=> string(18) "00.635.344/0001-77" ["ultima_atualizacao"]=> string(24) "2019-01-03T10:43:40.334Z" ["status"]=> string(2) "OK" ["tipo"]=> string(6) "MATRIZ" ["fantasia"]=> string(0) "" ["complemento"]=> string(0) "" ["efr"]=> string(0) "" ["motivo_situacao"]=> string(0) "" ["situacao_especial"]=> string(0) "" ["data_situacao_especial"]=> string(0) "" ["atividades_secundarias"]=> array(1) { [0]=> object(stdClass)#781 (2) { ["code"]=> string(10) "00.00-0-00" ["text"]=> string(14) "Não informada" } } ["capital_social"]=> string(7) "1000.00" ["extra"]=> object(stdClass)#798 (0) { } ["billing"]=> object(stdClass)#789 (2) { ["free"]=> bool(true) ["database"]=> bool(true) } } ["exception"]=> NULL }

and dd($receita):

JsonResponse {#820 ▼
  #data: "{"atividade_principal":[{"text":"Forma\u00e7\u00e3o de condutores","code":"85.99-6-01"}],"data_situacao":"25\/02\/2001","nome":"AUTO ESCOLA GUILHERMITTI & LOBANCO LTDA","uf":"SP","telefone":"(17) 3258-1649 \/ (17) 3258-1649","email":"[email protected]","qsa":[{"qual":"49-S\u00f3cio-Administrador","nome":"ELIANA CRISTINA GUILHERMITTI RODRIGUES"},{"qual":"49-S\u00f3cio-Administrador","nome":"SUZANA MARQUES LOBANCO"},{"qual":"49-S\u00f3cio-Administrador","nome":"AMARILDO APARECIDO RODRIGUES"},{"qual":"22-S\u00f3cio","nome":"VALDEMIR FRANCISCO DA COSTA"},{"qual":"49-S\u00f3cio-Administrador","nome":"ANDRE LUIS LOBANCO"}],"situacao":"ATIVA","bairro":"CENTRO","logradouro":"R CASTRO ALVES","numero":"1408","cep":"15.115-000","municipio":"BADY BASSITT","porte":"MICRO EMPRESA","abertura":"02\/06\/1995","natureza_juridica":"206-2 - Sociedade Empres\u00e1ria Limitada","cnpj":"00.635.344\/0001-77","ultima_atualizacao":"2019-01-03T10:43:40.334Z","status":"OK","tipo":"MATRIZ","fantasia":"","complemento":"","efr":"","motivo_situacao":"","situacao_especial":"","data_situacao_especial":"","atividades_secundarias":[{"code":"00.00-0-00","text":"N\u00e3o informada"}],"capital_social":"1000.00","extra":{},"billing":{"free":true,"database":true}} ◀"
  #callback: null
  #encodingOptions: 0
  +headers: ResponseHeaderBag {#819 ▶}
  #content: "{"atividade_principal":[{"text":"Forma\u00e7\u00e3o de condutores","code":"85.99-6-01"}],"data_situacao":"25\/02\/2001","nome":"AUTO ESCOLA GUILHERMITTI & LOBAN ▶"
  #version: "1.0"
  #statusCode: 200
  #statusText: "OK"
  #charset: null
  +original: {#792 ▶}
  +exception: null
}
marcellopato's avatar

@JOHNBRAUN - In PHP and Vue:

Cannot access protected property Illuminate\Http\JsonResponse::$data (View: /Users/marcellopato/Sites/primorossi/resources/views/contemplado/pessoaJ.blade.php)

JohnBraun's avatar

$receita->toArray()[‘data’][‘natureza_juridica’]; ?

JohnBraun's avatar

@MARCELLOPATO - And $receita[‘data’][‘natureza_juridica’]; then? Sorry, I am not at home otherwise I would’ve validated my replies.

marcellopato's avatar

@JOHNBRAUN - No problem John, I can say nothing but thanks! I´ve changed my mind and now I´m doing an AJAX request via AXIOS and it is working. Sorry to bother and thanks a lot for your help!

1 like

Please or to participate in this conversation.