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');
}
}
}
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….
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"
}
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/
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
}
}
]
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
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;
}
}
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.
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. :(
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;
I use username instead of id and it works:
http://graph.facebook.com/{username}?fields=cover
Parse the JSON accordingly.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With