Problems with Notice error: Undefined Property: stdClass:

Asked

Viewed 1,217 times

-2

Hello I’m new in IT and I’m developing an API pro Tiny that makes a query of tax notes but I’m having a problem showing these formatted results, I created a foreach which aimed to select only the information I need that would be the "number" and the "name".

   <!DOCTYPE html>
<head>
    <metacharset = "utf-8">
</head>
<html>
<body>

<?php


    $url = 'https://api.tiny.com.br//api2/notas.fiscais.pesquisa.php';
    $token = '314924f95c4eaf43f0657917defc742205c35317';//token esta invalido por segurança minha.
    $numero= '229199';
    $data = "token=$token&numero=$numero&formato=JSON";

    $retorno = enviarREST($url, $data);
    $content = json_decode($retorno);


    foreach ($content->retorno->notas_fiscais as $nota_fiscal){
        echo $nota_fiscal->numero. " - ".$nota_fiscal->nome."<br/>";
    }
    //var_dump($nota_fiscal);
    function enviarREST($url, $data, $optional_headers = null) {
    $params = array('http' => array(
        'method' => 'POST',
        'content' => $data
    ));

    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }

    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        throw new Exception("Problema com $url, $php_errormsg");
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
        throw new Exception("Problema obtendo retorno de $url, $php_errormsg");
    }

    return $response;
}

?>

But unfortunately I see myself with this mistake:

Notice: Undefined Property: stdClass::$numero in C: wamp64 www Apiteste buscaNF.php on line 21 Notice: Undefined Property: stdClass::$nome in C: wamp64 www Apiteste buscaNF.php on line 21

2 answers

1

you are trying to foreach the notas_fiscal property within an object of the stdClass class, which is a native PHP class used to store data temporarily without having to create a class just for that, however the property notas_fiscal does not exist in this object being traveled.

Use var_dump and check what the $content variable has before looping, it is possible that the values are stored in arrays inside obj and not properties, since the data source is a JSON.

Any doubt put the result of var_dump()

  • Hello, I took your advice and gave a var_dump() in $content and you were right the class did not exist there returning me that:C: wamp64 www Apiteste.php:19: Object(stdClass)[7] public 'return' => Object(stdClass)[1] public 'status_processing' => string '3' (length=1) public 'status' => string 'OK' (length=2) public 'pagina' => int 1 public 'numero_paginas' => int 1 public 'notas_fiscais' => array (size=1) 0 => Object(stdClass)[6] ...

  • more when I give a var_dump in return I have what I want: '{"return":{"status_processing":"3","status":"OK","page":1,"numero_pages":1,"tax notas_tax":[{"nota_fiscal":{"id":"606255396","type":"S","series":"1","number":"229199","numero_ecommerce":null,"data_issuance":"07/02/2020","name":"J ANFLOR ELETRONICOS EIRELI","client":{"name":":"J ANFLOR ELETRONICOS EIRELI","tipo_pessoa":"J","cpf_cnpj":"08.218.472/0001-27","ie":"0570248981",""address":"AV DORIVAL CANDIDO LUZ DE OLIVEIRA","numero":"2875","complement":"""","neighborhood":"SAO JERONIMO","cep":"94.040-001"

  • but I don’t know why I can’t show this by the variable $content. using json_decode().

1


This error means that you are trying to access an attribute of something that is not an object.

For example:

$xpto->atributo;

If this $xpto is null or not an object will generate this error when trying to catch the propriedade ->atributo.

When we have a model return with relationships, if the relationship does not exist or is not configured correctly, a null will be returned, then we will have the same problem. For example:

$user->role->name;

If this user does not have a role (role), it will be returned null, so we would not be able to access the property ->null name. In that case, to test we could make a:

dd($user->role);

And see if it’s not returning null.

Do this for the relationships you are returning to the view.

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.