2013-05-21 38 views
0

(我請教Trouble looping through an array created from a foursquare JSON feed,但代碼列在那裏一直沒有工作對我來說)四方場地:API和json_decode問題

我操作四方與多個場館的業務。我們希望爲每個場地提供一些統計數據(例如簽入總數等)到我們網站上的各個頁面。所以我註冊了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"

回答