使用PHP解析Yelp API的JSON响应

时间:2022-05-14 21:25:28

I can't seem to parse information sent by the Yelp API. Here's the output: http://www.coroomer.com/apartments/yelp.php.

我似乎无法解析Yelp API发送的信息。这是输出: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. Formatted as in it looks like the review threads on Yelp. Any help is appreciated.

问题是我无法获得正确的“格式化”输出。格式化在它看起来像Yelp上的审查线程。任何帮助表示赞赏。

1 个解决方案

#1


1  

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:

1.首先修复警告和通知。不要尝试在没有索引的单引号或双引号的情况下访问数组,因为PHP会尝试将它们解析为CONSTANTS。这将导致:

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.转储没有索引业务的任何数组,你想在这里迭代什么?

#1


1  

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:

1.首先修复警告和通知。不要尝试在没有索引的单引号或双引号的情况下访问数组,因为PHP会尝试将它们解析为CONSTANTS。这将导致:

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.转储没有索引业务的任何数组,你想在这里迭代什么?