0

I was following the documentation of Guzzle and got stuck on json response. This is my code

require 'vendor/autoload.php';
use GuzzleHttp\Client;

$client = new Client();
$response = $client->get('http://httpbin.org/get');
$json = $response->json();
var_dump($json[0]['origin']);

When I run this file I get the error

Notice: Undefined offset: 0 in C:\xampp\htdocs\guzzle\config.php on line 8
NULL 

Why am I getting undefined offset?

Priya Rawat
  • 259
  • 1
  • 5
  • 12

1 Answers1

0

Previous stack overflow questions that can provide this same question and answer, can be found here and here.

From the Guzzle Docs:

Guzzle uses the json_decode() method of PHP and uses arrays rather than stdClass objects for objects.

PHP is trying to access the key 0 of the $json array. Instead of finding a value it encounters an undefined offset for [0] and throws the error.

Community
  • 1
  • 1
Shaun Bramley
  • 1,989
  • 11
  • 16