Invalid argument supplied for foreach () - Array

0

Good morning.

I'm trying to consume a json in PHP but I'm having this problem:

  

Warning: Invalid argument supplied for foreach () in   D: \ xampp \ htdocs \ api \ consume \ index.php on line 7

Array code - JSON

link

PHP code

<?php
$json_file = file_get_contents("http://localhost/api/carne/retornofim.php");

$json_str = json_decode($json_file, true);
$itens = $json_str['nodes'];

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

link

Att

    
asked by anonymous 24.04.2018 / 13:33

1 answer

1
  

Warning: Invalid argument supplied for foreach () in   D: \ xampp \ htdocs \ api \ consume \ index.php on line 7

This message occurs because the variable $itens is not an array, or a possible object to be used inside the foreach, and analyzing your json does not exist these keys that are in the code (nodes or title).

Then before using the foreach, or try to fetch some key place:

$json_file = file_get_contents("http://localhost/api/carne/retornofim.php");
var_export($json_file);
die();

So you'll be able to see the feedback before manipulating the data.

To show more information:

header('Content-Type: text/plain');
error_reporting(~0); ini_set('display_errors', 1);
$url = 'http://localhost/api/carne/retornofim.php';
$results = file_get_contents($url);
var_dump($http_response_header, $results);
die();

Source: PHP: "file_get_contents ()" returning NULL from content-verified URL

    
24.04.2018 / 13:45