Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Facebooks' Cover Photo via PHP SDK & Graph API

I'd like to know if there is a simple way of retrieving the Cover photo from a user in my application, via the Facebook php sdk.

I managed to retrieve the cover photo id of the album of the cover photo album, but my solution only works partially. (As you can see, it's not efficient, and If more albums will be added, and moved to other pages, thus this will not work).

    $albums = $facebook->api('/me/albums','GET');
    for($i = 0;$i<count($albums['data']);$i++){
        for($j = 0;$j<count($albums['data'][$i]);$j++){
            if($albums['data'][$i]['name'] == "Cover Photos"){
                echo $facebook->api('/me/albums/'.$albums['data'][$i]['cover_photo'],'GET');
            }
        }
    }
like image 904
funerr Avatar asked Nov 14 '11 17:11

funerr


People also ask

How do I copy a Facebook cover photo?

The cover photo is at the top of the page and will open in the image viewer. Right-click the image. A menu will appear on your cursor. Click Save image as….


10 Answers

I think the easiest is to use the graph api:

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

and parse the returned JSON.

{
  "cover": {
    "id": "XXX", 
    "source": "https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-ash4/311205_989690200741_1231438675_n.jpg", 
    "offset_y": 66
  }, 
  "id": "XXXXXX"
}
like image 138
Omri Avatar answered Oct 01 '22 10:10

Omri


Facebook seems to have added the Cover field to User Object recently

https://graph.facebook.com/facebookuserid?fields=cover will give you the user cover pic Details

Facebook Doc Link : http://developers.facebook.com/docs/reference/api/user/

like image 43
Priyadarshan Salkar Avatar answered Oct 01 '22 09:10

Priyadarshan Salkar


Facebook has a really poor and outdated documentation... At the moment there isn't a proper way to do this via Graph API, but with FQL it is possible.

SELECT pic_cover FROM user WHERE uid='yourUserId'

The field pic_cover isn't documented but available. The response should look like this:

[
 {
  "pic_cover": {
   "cover_id": someId,
   "source": "someUrl",
   "offset_y": someOffset
  }
 }
]
like image 30
Geeko Avatar answered Oct 01 '22 09:10

Geeko


The latest versions of Graph API (2.4 is current at the time of writing) support 'picture' field that returns user's profile picture. It's worth checking.

/<user-id>?fields=id,name,picture
like image 29
user3498393 Avatar answered Oct 01 '22 08:10

user3498393


I think it is better to ask type of album then name.

$albums = $facebook->api("/me/albums");
$album_id = ""; 
foreach($albums["data"] as $item){
//echo $item["id"]." - ".$item["name"]." - ".$item["type"]." - ".$item["cover_photo"]."<br/>";
    if($item["type"] == "profile"){
        $album_id = $item["id"];
    $profile_picture_id = $item["cover_photo"];
    break;
    }
}
like image 29
Joe Bobson Avatar answered Oct 01 '22 08:10

Joe Bobson


Looks like at this stage, pending updated documentation from facebook, that your method is the only "sure fire" way to retrieve the cover of a users timeline.
If you find this method to be slow or inefficient - maybe you could try running it as a cron job and having a minimal update delay.. eg. run the cron twice a day (maybe even more), maybe handle two or three users at a time and have the cron running every 2 minutes... Not a solution per-say; more of a suggestion.

like image 24
Lix Avatar answered Oct 01 '22 08:10

Lix


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' to a php varible?

Sr, I can't reply so i post here. :(

like image 38
Zen Nguyễn Avatar answered Oct 01 '22 10:10

Zen Nguyễn


it tries to automatically recover from the image through php

$json = file_get_contents("https://graph.facebook.com/PROFILE_ID?fields=cover");
$obj = json_decode($json);
echo $obj->cover ->source;
like image 42
Jorge Mora Pérez Avatar answered Oct 01 '22 10:10

Jorge Mora Pérez


I use username instead of id and it works:

http://graph.facebook.com/{username}?fields=cover

Parse the JSON accordingly.

like image 31
Spenciefy Avatar answered Oct 01 '22 10:10

Spenciefy


I have used https://graph.facebook.com/facebookuserid?fields=cover link but it did not work. When I search Facebook SDK documentation, I see the answer that works. I hope this will be helpful for you.

[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@?fields=cover", [user objectForKey:@"id"]]
                      completionHandler:^(
                                          FBRequestConnection *connection,
                                          id result,
                                          NSError *error
                                          ) {
                          NSLog(@"%@",[[result objectForKey:@"cover"]objectForKey:@"source"]);
                          /* handle the result */
                      }];

Result is a type of a Dictionary. You can see the link of user's cover photo under "cover" tag and its "source" tag also.

like image 26
Begum Avatar answered Oct 01 '22 09:10

Begum