未定义的偏移量:在json响应的var_dump()期间Guzzle中为0(Undefined offset: 0 in Guzzle during var_dump() on json response)

我正在关注Guzzle的文档并且卡在了json的回应上。 这是我的代码

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']);

当我运行此文件时,我收到错误

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

为什么我得到未定义的偏移量?

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?

最满意答案

可以在此处和此处找到可以提供相同问题和答案的先前堆栈溢出问题。

来自Guzzle文档 :

Guzzle使用PHP的json_decode()方法,并使用数组而不是stdClass对象作为对象。

PHP正在尝试访问$ json数组的键0。 它没有找到值,而是遇到[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.

未定义的偏移量:在json响应的var_dump()期间Guzzle中为0(Undefined offset: 0 in Guzzle during var_dump() on json response)

我正在关注Guzzle的文档并且卡在了json的回应上。 这是我的代码

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']);

当我运行此文件时,我收到错误

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

为什么我得到未定义的偏移量?

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?

最满意答案

可以在此处和此处找到可以提供相同问题和答案的先前堆栈溢出问题。

来自Guzzle文档 :

Guzzle使用PHP的json_decode()方法,并使用数组而不是stdClass对象作为对象。

PHP正在尝试访问$ json数组的键0。 它没有找到值,而是遇到[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.