如何从Facebook图形API获取Facebook封面网址

时间:2013-06-19 05:29:54

标签: php facebook facebook-graph-api

我使用:http://graph.facebook.com/{user_id or page_id}?fields=cover

浏览器返回:

{
  "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_y转换为PHP个变量?

我的功能:

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;
}

1 个答案:

答案 0 :(得分:0)

要在PHP中解析JSON,您需要使用json_decode(returned_facebook_json),然后在访问Object时访问数据,例如

$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;