-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
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] ...
– whillian
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"
– whillian
but I don’t know why I can’t show this by the variable $content. using json_decode().
– whillian