0
The external url has a data structure that I receive in my code and it is being received as a string JSON
, I tried to decode it but I didn’t get any results.
http://publisher.windi.com.br/manager/estoquejson/? hash=8d37ddfa64d1e0a2d9cb887c2ed86619&l=8910809
I hope to receive the string JSON
and convert it into the format below:
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
My PHP
<?php
ini_set("allow_url_fopen", 1);
header("Content-type: text/html; charset=iso-8859-1");
$urlPath = "http://publisher.windi.com.br/manager/estoquejson/?hash=8d37ddfa64d1e0a2d9cb887c2ed86619&l=8910809";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $urlPath);
$result = curl_exec($ch);
curl_close($ch);
// Imprimo
echo $result;
// Aqui sai como string, tentei converter a estrutura para array mas sem sucesso.
// { "vehicles": [ { "id": 164999, "highlight": 0, "anoFabricacao": 2011, "anoModel": 2012, "km": 86000, "doors": 4, "valorVenda": 29990.0, "dateTetdate": "Apr 27, 2019 12:00:00 PM", ................
Knowing that:
PHP> = 5.2.0 presents a function, json_decode, that decodes a JSON sequence in a PHP variable. By default, it returns an object. The second parameter accepts a boolean which, when set to true, tells it to return objects as associative matrices. You can learn more about json_decode in PHP documentation.
The way out for the change I made does not result in anything.
<?php
ini_set("allow_url_fopen", 1);
header("Content-type: text/html; charset=iso-8859-1");
$urlPath = "http://publisher.windi.com.br/manager/estoquejson/?hash=8d37ddfa64d1e0a2d9cb887c2ed86619&l=8910809";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $urlPath);
$result = curl_exec($ch);
curl_close($ch);
// Decodifiquei a string `JSON` esperando algo, mas a exibição foi em branco.
$obj = json_decode($result, true);
print_r($obj);
// Ou com var_dump(); retornou NULL
var_dump($obj);
I kept IF and PRINT_R for information purposes only, as well as json_last_error for being a development environment, with I can continue what I have to do.
– ElvisP