开发者

Parsing JSON response using PHP for Yelp API

I can't seem to parse information sent by the Yelp API. Here's the 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 correctl开发者_如何学Cy "formatted" output. Formatted as in it looks like the review threads on Yelp. Any help is appreciated.


It's not clear what exactly you are asking. However...

1. Fix your warnings and notices first. Do not try to access arrays without single or double quotes around indexes, because PHP will try to resolve them as 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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜