0

我用: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"
}

如何获取字段和source变量?offset_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;
}
4

1 回答 1

0

要在 PHP 中解析 JSON,您需要使用json_decode(returned_facebook_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;
于 2013-06-19T06:11:45.460 回答