我查詢Instagram的API來使用此代碼返回JSON:用PHP基本的循環構建JSON
$instagramClientID = '9110e8c268384cb79901a96e3a16f588';
$api = 'https://api.instagram.com/v1/tags/zipcar/media/recent?client_id='.$instagramClientID; //api request (edit this to reflect tags)
$response = get_curl($api); //change request path to pull different photos
所以我要來解碼JSON
if($response){
// Decode the response and build an array
foreach(json_decode($response)->data as $item){
...
所以現在我想重新格式化所述陣列的內容到一個特定的JSON格式(以GeoJSON)的代碼將是大致這樣的:
array(
'type' => 'FeatureCollection',
'features' => array(
array(
'type' => 'Feature',
'geometry' => array(
'coordinates' => array(-94.34885, 39.35757),
'type' => 'Point'
), // geometry
'properties' => array(
// latitude, longitude, id etc.
) // properties
), // end of first feature
array(...), // etc.
) // features
)
然後用json_encode
將其全部返回到一個不錯的json文件中以在服務器上緩存。
我的問題是如何使用上面的代碼來循環json?數組/ json的外部結構是靜態的,但內部需要更改。
什麼是你的輸入和預期的輸出? – six8 2012-04-18 00:37:46
輸入將會是標準的instagram json return:http://instagr.am/developer/endpoints/tags/#get_tags_media_recent,我會將它重新格式化爲GeoJson,並且看起來非常像這樣:http:// alwaysbecreating。 org/zip.json(re:@Cixate) – 2012-04-18 00:38:57