简体   繁体   中英

How can I get facebook cover url from Facebook graph API

I use: http://graph.facebook.com/{user_id or page_id}?fields=cover

Return on browser:

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

How can I get field source and offset_y to PHP variables?

My function:

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

To parse the JSON in PHP you are required to use json_decode(returned_facebook_json) and then access the data as you access a Object, for example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM