0

(我已经咨询了Trouble looping through an array created from afoursquare JSON feed,但那里列出的代码对我不起作用)

我为一家拥有多个场所的企业经营 Foursquare。我们希望将每个场地的一些统计数据(例如签到总数等)提取到我们网站上的各个页面中。所以我注册了 Foursquare 的 API 并在我的 Foursquare 帐户中创建了一个应用程序。

我在我们网站上的 PHP 代码已成功连接并从 Foursquare 拉取场地信息——我的问题是到目前为止我无法输出解码后的 JSON 的内容。我尝试了关联数组方法(json_decode($response,true))和非数组方法(json_decode($response))都无济于事。

也就是说,我的目标是专门回应一些统计字段,如 checkinsCount。有人可以告诉我我在下面的 for 循环中做错了什么以及如何引用输出中的特定字段,例如 checkinsCount?

在此先感谢您的帮助!

$curlhandle = curl_init();
curl_setopt($curlhandle, CURLOPT_URL, "https://api.foursquare.com/v2/venues/VENUEID?               client_id=CLIENTID&client_secret=CLIENTSECREAT");
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curlhandle);
//echo $response;
// THE ABOVE WORKS -- I'M ABLE TO GET A RESPONSE

curl_close($curlhandle);

$json = json_decode($response, true);
//var_dump($json);
// THE ABOVE WORKS -- I'M ABLE TO SUCCESSFULLY LIST THE VARIABLES AND VALUES

foreach ( $json['response']['groups'][0]['items'] as $items )
// THIS IS NOT WORKING -- I GET BLANK OUTPUT

//THESE ARE THE FIELDS I WANT TO ECHO
mayor
stats->checkinsCount
stats->usersCount 

这是我的 var_dump 中显示的内容片段:

["categories"]=> array(2) { [0]=> array(7) { ["id"]=> string(24) "4bf58dd8d48988d1e0931735" ["name"]=> string(11) "Coffee Shop" ["pluralName"]=> string(12) "Coffee Shops" ["shortName"]=> string(11) "Coffee Shop" ["icon"]=> string(57) "REMOVEDURLPREFIX/img/categories/food/coffeeshop.png" ["parents"]=> array(1) { [0]=> string(4) "Food" } ["primary"]=> bool(true) } [1]=> array(6) { ["id"]=> string(24) "4bf58dd8d48988d16d941735" ["name"]=> string(5) "Café" ["pluralName"]=> string(6) "Cafés" ["shortName"]=> string(5) "Café" ["icon"]=> string(51) "REMOVEDURLPREFIX/img/categories/food/cafe.png" ["parents"]=> array(1) { [0]=> string(4) "Food" } } } ["verified"]=> bool(true) ["restricted"]=> bool(true) ["stats"]=> array(3) { ["checkinsCount"]=> int(142) ["usersCount"]=> int(35) ["tipCount"]=> int(4) } ["url"]=> string(18) "http://denison.edu" ["likes"]=> array(2) { ["count"]=> int(0) ["groups"]=> array(0) { } } ["specials"]=> array(0) { } ["photos"]=> array(3) { ["count"]=> int(1) ["groups"]=> array(2) { [0]=> array(4) { ["type"]=> string(7) "checkin" ["name"]=> string(24) "Friends' check-in photos" ["count"]=> int(0) ["items"]=> array(0) { } } [1]=> array(4) { ["type"]=> string(5) "venue" ["name"]=> string(12) "Venue photos" ["count"]=> int(1) ["items"]=> array(1) { [0]=> array(7) { ["id"]=> string(24) "5122de9ce4b0a8206c1158e5"

4

1 回答 1

0

仔细看看 Foursquare 的响应格式。例如,检查https://developer.foursquare.com/docs/explore#req=venues/40a55d80f964a52020f31ee3 - 注意没有返回“组”对象。

您可能想使用$json['response']['venue']['mayor']$json['response']['venue']['stats']开始。

于 2013-05-28T23:30:07.117 回答