简体   繁体   English

使用 Yelp API 的 PHP 解析 JSON 响应

[英]Parsing JSON response using PHP for Yelp API

I can't seem to parse information sent by the Yelp API.我似乎无法解析 Yelp API 发送的信息。 Here's the output: http://www.coroomer.com/apartments/yelp.php .这是 output: http://www.coroomer.com/apartments/yelp.php

Here is the segment of the code I am having trouble with:这是我遇到问题的代码段:

// Send Yelp API Call
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);

// Handle Yelp response data
$obj = json_decode($response,true);

// Print it for debugging
//print_r($obj);

echo var_dump($obj);

if (isset($bus)) {
foreach($obj[businesses] as $bus){
    echo $bus[name];
    echo $bus[reviews];
    }
}

The problem is that I can't get a correctly "formatted" output.问题是我无法获得正确“格式化”的 output。 Formatted as in it looks like the review threads on Yelp.格式化为它看起来像 Yelp 上的评论线程。 Any help is appreciated.任何帮助表示赞赏。

It's not clear what exactly you are asking.目前尚不清楚您到底在问什么。 However...然而...

1. Fix your warnings and notices first. 1.首先修复您的警告和通知。 Do not try to access arrays without single or double quotes around indexes, because PHP will try to resolve them as CONSTANTS.不要尝试在索引周围没有单引号或双引号的情况下访问 arrays,因为 PHP 会尝试将它们解析为 CONSTANTS。 Which will lead to:这将导致:

a.一个。 Slower runtime运行时间较慢

b.湾。 Headaches, if a constant exists with that index头痛,如果该索引存在常量

Change this code:更改此代码:

foreach($obj[businesses] as $bus){
    echo $bus[name];
    echo $bus[reviews];

to

foreach($obj['businesses'] as $bus){
    echo $bus['name'];
    echo $bus['reviews'];

2. The dump doesn't have any array with the index businesses , what are you trying to iterate over here? 2.转储没有任何索引businesses数组,你想在这里迭代什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM