Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON显示在屏幕上,而不是推送到变量_Php_Json - Fatal编程技术网

Php JSON显示在屏幕上,而不是推送到变量

Php JSON显示在屏幕上,而不是推送到变量,php,json,Php,Json,我一直在尝试编写一段代码,使用API从一家网店检索条目。使用此代码 <?php $ch = curl_init('https://api.mijnwebwinkel.nl/v1/orders?language=nl_NL&limit=10&start_date=2017-04-10&end_date=2017-04-20&format=json&ordering=asc&partner_token=xxx&token=xxx'); c

我一直在尝试编写一段代码,使用API从一家网店检索条目。使用此代码

<?php
$ch = curl_init('https://api.mijnwebwinkel.nl/v1/orders?language=nl_NL&limit=10&start_date=2017-04-10&end_date=2017-04-20&format=json&ordering=asc&partner_token=xxx&token=xxx');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

// execute post
$result = curl_exec($ch);

// close connection
curl_close($ch);
?>
导致

为foreach()提供的参数无效


在print_r和var_dump的输出中,“stdClass Object”表示数据类型,而不是变量的名称

请尝试改用此代码

foreach ($data as $object)
{
    echo "items:". $object['number'] ."\n";
};

正如我在评论中提到的,cURL默认输出响应。要防止出现这种情况,应将
CURLOPT\u RETURNTRANSFER
选项设置为
true

更进一步-
stdClass对象
在您的输出中不是一个键。它是一个类的名称。因此,数组的第一个元素是类
stdClass
object

在数组上迭代是:

foreach ($data as $object)
{
    // `$object` is a stdClass object
    // accessing object properties is done with `->`
    echo "items:". $object->number ."\n";
};
或者,您可以将第二个参数设置为
true
,对您的响应进行
json\u解码。所有项目都将是数组:

json_decode($result, true);

默认情况下,CURL输出结果。要防止输出,请使用
RETURNTRANSFER
选项。阅读手册有时会有所帮助。
json_decode($result, true);