从Yelp API获取数据

时间:2014-05-22 15:21:13

标签: php yelp

我开始研究Yelp API。当我发送搜索请求时,我得到一个数组$ response返回的数据。所以,如果我像这样输出

echo '<pre>';
print_r($response);
echo '</pre>';

我看到以下格式的结果

stdClass Object
(
    [message] => stdClass Object
        (
            [text] => OK
            [code] => 0
            [version] => 1.1.1
        )

    [businesses] => Array
        (
            [0] => stdClass Object
                (
                    [rating_img_url] => http://s3-media2.ak.yelpcdn.com/assets/2/www/img/99493c12711e/ico/stars/v1/stars_4_half.png
                    [country_code] => US
                    ...
                 )
         )
)

所以,让我们说我想获得国家代码,不应该用类似的东西来获取它吗?

echo $response['businesses'][0]->country_code;

我没有得到任何结果。我错过了什么?

1 个答案:

答案 0 :(得分:1)

echo $response->businesses[0]->country_code;

businesses是属性,而不是数组元素。

stdClass Object以下的所有内容都是属性。

=> Array以下的所有内容都是数组元素。

让我猜一下,$response = json_decode(...);

您可以通过设置第二个参数true来告诉此函数返回关联数组而不是对象:

$response = json_decode(..., true);

然后中的值>:

echo $response['businesses'][0]['country_code'];