Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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
cURL/PHP/JSON-为什么我无法解析结果?_Php_Arrays_Json_Curl - Fatal编程技术网

cURL/PHP/JSON-为什么我无法解析结果?

cURL/PHP/JSON-为什么我无法解析结果?,php,arrays,json,curl,Php,Arrays,Json,Curl,我使用以下代码从带有JSON结果的API中提取数据 // Open connection $ch = curl_init(); // Make the curl call curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: appl

我使用以下代码从带有JSON结果的API中提取数据

// Open connection
$ch = curl_init();

// Make the curl call
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


  $data = json_decode($head,true); 
   foreach($data as $item) {
    $name = $item['name'];
    $description = $item['description'];
    echo "NAME: $name DESCRIPTION: $description";

 }
如果我使用print_r($head),我会看到所有的结果,所以我知道url、用户名、密码都很好,但是代码末尾的echo语句不会产生结果

// Open connection
$ch = curl_init();

// Make the curl call
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$head = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


  $data = json_decode($head,true); 
   foreach($data as $item) {
    $name = $item['name'];
    $description = $item['description'];
    echo "NAME: $name DESCRIPTION: $description";

 }
下面是我打印r($head)时JSON的示例。至少有一部分

{"events":[{"eventId":5183191,"name":"The Rhythm Rockets - FREE","description":"Summer Concert Series - The Rhythm Rockets . .\nFREE. Look for a YELLOW balloon. Bring a chair. PLEASE BE ON TIME (6:00 PM!) TO SIT WITH THE GROUP. Allow time for street parking. Expect a crowd. Meetup at 6pm - Showtime is 7pm. Food, wine and beer will be on sale.\nIn case of inclement weather, concerts will be held indoors at the Lincoln Center, 935 Maple Avenue. For weather updates, call[masked] after 5:30 pm. If moved inside, there is no way we can sit together.","primaryImage":{"smallImageUrl":"https://domainnamestorage.blob.core.windows.net/portalimages/portalimages/71021444-d72d-456c-a419-36b61ead5259.small.png","mediumImageUrl":"https://domainnamestorage.blob.core.windows.net/portalimages/portalimages/71021444-d72d-456c-a419-36b61ead5259.medium.png","largeImageUrl":"https://domainnamestorage.blob.core.windows.net/portalimages/portalimages/71021444-d72d-456c-a419-36b61ead5259.large.png"},"categories":[{"categoryId":2,"parentCategoryId":null,"name":"Performing Arts"},{"categoryId":12,"parentCategoryId":null,"name":"Food & Drink"},{"categoryId":17,"parentCategoryId":2,"name":"Music"},{"categoryId":75,"parentCategoryId":12,"name":"Drinks"},{"categoryId":100,"parentCategoryId":17,"name":"Concerts"}],"location":{"latitude":41.792683,"longitude":-88.011765,"city":"Downers Grove","state":"IL","address":"1000 Grove St","country":"United States","locationName":"Fishel Park"},"instances":[{"startDateTime":"2018-08-21T18:00:00Z","endDateTime":null,"hasTime":true,"allDay":false}],"price":{"lowerPrice":null,"upperPrice":null,"free":false,"description":null},"lastUpdatedDate":"2018-07-26T02:20:49.686Z","popularity":null,"interest":50,"links":[{"isSponsor":false,"name":"Meetup","linkUrl":"https://www.meetup.com/Im-Not-Dead-Yet-50-plus/events/252406255/","logoUrl":null},{"isSponsor":true,"name":"I'm Not Dead Yet --- 50+ Meetup","linkUrl":"http://Meetup.com/Im-Not-Dead-Yet-50-plus","logoUrl":null},{"isSponsor":true,"name":"I'm Not Dead Yet --- 50+ Meetup","linkUrl":"http://Meetup.com/Im-Not-Dead-Yet-50-plus","logoUrl":null}],"previousEventIds":[],"seriesStart":"2018-08-21T18:00:00Z","seriesEnd":null,"url":"http://portal.domainname.com/m/FireVite#!/details/THE-RHYTHM-ROCKETS-FREE/5183191/2018-08-21T18","handPicked":false,"customField":null,"contact":{"organization":null,"name":null,"email":null,"phone":null},"media":null}
print\u r()
字符串
没有意义,只对
数组
对象
有意义

它可能与此非常相似(可能您必须使用
$data['events']

使用
$data=json\u decode($response)时它将是
$data->events

$data = json_decode($response, true);

// die('<pre>'.print_r($data, true).'</pre>');

foreach($data['events'] as $key => $item) {

    // $event_id = $data['events'][$key]['eventId'];
    $event_id = $item['eventId'];

    // $name = $data['events'][$key]['name'];
    $name = $item['name'];

    // $desc = $data['events'][$key]['description'];
    $desc = $item['description'];

    echo "KEY: $key, ID: $event_id, NAME: $name, DESCRIPTION: $desc";
}
$data=json\u decode($response,true);
//模具(''.print\u r($数据,正确)。'';
foreach($data['events']作为$key=>$item){
//$event_id=$data['events'][$key]['eventId'];
$event_id=$item['eventId'];
//$name=$data['events'][$key]['name'];
$name=$item['name'];
//$desc=$data['events'][$key]['description'];
$desc=$item['description'];
echo“KEY:$KEY,ID:$event\u ID,NAME:$NAME,DESCRIPTION:$desc”;
}
这些内联注释只是指示实际访问的内容。

print\u r()
对于
字符串
没有意义,而仅对于
数组
对象

它可能与此非常相似(可能您必须使用
$data['events']

使用
$data=json\u decode($response)时它将是
$data->events

$data = json_decode($response, true);

// die('<pre>'.print_r($data, true).'</pre>');

foreach($data['events'] as $key => $item) {

    // $event_id = $data['events'][$key]['eventId'];
    $event_id = $item['eventId'];

    // $name = $data['events'][$key]['name'];
    $name = $item['name'];

    // $desc = $data['events'][$key]['description'];
    $desc = $item['description'];

    echo "KEY: $key, ID: $event_id, NAME: $name, DESCRIPTION: $desc";
}
$data=json\u decode($response,true);
//模具(''.print\u r($数据,正确)。'';
foreach($data['events']作为$key=>$item){
//$event_id=$data['events'][$key]['eventId'];
$event_id=$item['eventId'];
//$name=$data['events'][$key]['name'];
$name=$item['name'];
//$desc=$data['events'][$key]['description'];
$desc=$item['description'];
echo“KEY:$KEY,ID:$event\u ID,NAME:$NAME,DESCRIPTION:$desc”;
}

这些内联注释只是指示实际访问的内容。

解码后,$data将有一个名为events的数组,每个元素都是您试图查看的数组。尝试:


解码后,$data将有一个名为events的数组,每个元素都是您试图查看的数组。尝试:


如果没有有效负载,Content-type:header就没有意义。可能建议改为使用Accept:。如果没有有效负载,则Content type:标头没有意义。接受:可能是可取的。毫无意义,
print\r()
对字符串很好。这很好,但请再次检查上面的JSON示例。。。我添加了更多正在检索的数据。。。如何补偿数组,如“primaryImage”和“Location”。。。当我使用您为这些字符串变量提供的相同代码时,所有字符串变量只返回“array”。@Brad这样做的具体目的是什么???@disruptekz这样的JSON响应可能包含嵌套的数组/对象,这些数组/对象需要在嵌套的循环中解析,就像正在处理的结构的最顶层一样。胡说,
print\u r()
适用于字符串。这很好,但请再次检查上面的JSON示例。。。我添加了更多正在检索的数据。。。如何补偿数组,如“primaryImage”和“Location”。。。当我使用您为这些字符串变量提供的相同代码时,所有字符串变量只返回“array”。@Brad这样做的具体目的是什么???@disruptekz这样的JSON响应可能包含嵌套的数组/对象,需要在嵌套的循环中解析,就像正在处理的结构的最顶层一样。