2013-06-19 61 views
0

我使用:在瀏覽器http://graph.facebook.com/{user_id or page_id}?fields=cover我怎樣才能獲得Facebook的封面網址從Facebook圖形API

返回:

{ 
    "cover": { 
    "id": "XXX", 
    "source": "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-ash4/311205_989690200741_1231438675_n.jpg", 
    "offset_y": 66 
    }, 
    "id": "XXXXXX" 
} 

我怎樣才能獲得場sourceoffset_yPHP變量?

我的功能:

function cover_img($fb_user_name) { 
    $cover_data = 'https://graph.facebook.com/'.$fb_user_name.'?fields=cover'; 
    $JSONString = file_get_contents($cover_data); 
    $parsedJSON = json_decode($JSONString); 
    echo $source = $parsedJSON->cover->source; 
    echo $offset_y = $parsedJSON->cover->offset_y; 
} 

回答

0

爲了解析在PHP中,你需要使用json_decode(returned_facebook_json),然後爲你訪問對象訪問數據的JSON,例如

$JSONString=file_get_contents('http://graph.facebook.com/{user_id or page_id}?fields=cover'); 
$parsedJSON = json_decode($JSONString); 
echo $source = $parsedJSON->cover->source; 
echo $offset_y = $parsedJSON->cover->offset_y; 
+0

對不起,我不t知道得到** returned_facebook_json_string **在 'json_decode(returned_facebook_json_string);' 請告訴我如何? –

+0

你在使用Graph API嗎? –

+0

是的!我使用'http://graph.facebook.com/{user_id或page_id}?fields = cover'從一個答案在stackoverflow,但我不知道從它得到PHP變量:( 如何獲得'returned_facebook_json_string'使用JSON解碼就像你的答案? –

相關問題