Receive external JSON data from PHP

Asked

Viewed 48,813 times

13

I am trying to receive an external JSON file via PHP. I am doing so:

<?php

$json_file = file_get_contents(
     "http://www.moonwalk.com.br/api/VitrineDeProdutos/58e903df295cbc5c4639434d4c35090d");

$json_str = json_decode($json_file, true);

$itens = $json_str->nodes;

foreach ( $itens as $e ) 
    { echo "$e->title<br>"; } 
?>

But I’m not getting any results.

The external JSON files are at this address.

I’m doing something wrong?

  • Eu não posso responder, mais essa é uma boa formas, veja uma api de cotação como exemplo: <?php &#xA;$json_str = file_get_contents("https://economia.awesomeapi.com.br/json/all/USD-BRL,EUR-BRL"); &#xA;$jsonObj = json_decode($json_str);&#xA;$usd = $jsonObj->USD;&#xA;$eur = $jsonObj->EUR; ?>

3 answers

8


The second parameter of json_decode() is to force the output of the Decode to the structure of an associative array.

Your code is correct if you don’t have it true in the json_decode.

If you have true then you should expect an associative array, in which case you can use:

$json_file = file_get_contents("http://www.moonwalk.com.br/api/VitrineDeProdutos/58e903df295cbc5c4639434d4c35090d");   
$json_str = json_decode($json_file, true);
$itens = $json_str['nodes'];

foreach ( $itens as $e ) 
    { echo $e['title']."<br>"; } 

Example

6

Get the result by removing the true of json_decode().

json_decode($json_file);

json_decode ( string $json [, bool $Assoc = false [, int $Depth = 512 [, int $options = 0 ]]] ) php.net

The second parameter forces the return of json_decode() to be an associative array, while in the lines below you were trying to access an object.

$itens = $json_str->nodes;

If you have an error in the conversion use the functions json_last_error() (since php5.3) and json_last_error_msg() (since php5.5) to detect the cause.

  • 1

    I read the documentation and it worked like this : json_decode($file); and then json_decode($file, true);

0

You want to receive a json object like this?

{
    "nome" : "ra"
}

Example and submission (POST)

<?php

$request = new HttpRequest();
$request->setUrl('http://localhost:8080/usuariosapi.php');
$request->setMethod(HTTP_METH_POST);

$request->setQueryData(array(
  'acao' => 'consultarpornome'
));

$request->setHeaders(array(
  'postman-token' => 'd26d9873-4dda-705f-55a3-512f4acd3e18',
  'cache-control' => 'no-cache'
));

$request->setBody('{
    "nome" : "ra"
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

In the target PHP file you use this command to retrieve the JSON object

header('Content-Type: application/json; charset=utf-8');  

$json = file_get_contents('php://input');
$obj = json_decode($json);

I hope it helps you

  • That one new HttpRequest(); is which library?

  • 1

    This http://php.net/manual/it/class.httprequest.php, but only one example could be an ajax upload as well.

Browser other questions tagged

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