funerr
funerr

Reputation: 8166

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

Upvotes: 12

Views: 34482

Answers (12)

Rohit
Rohit

Reputation: 1

function getCover() {
    FB.api('/me?fields=cover', function(response) {
       document.getElementById('cover').innerHTML =
          "<img src='" + response.cover['source'] + "' alt='' />"

    });
}

Upvotes: 0

user3498393
user3498393

Reputation: 84

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

Upvotes: 4

Simon
Simon

Reputation: 19938

This is how I extracted my cover pic using Jackson and the Graph API.

        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(
                            JSONObject object,
                            GraphResponse response) {
                        ObjectMapper mapper = new ObjectMapper();
                        try {
                            JsonNode actualObj = mapper.readTree(String.valueOf(object));
                            JsonNode cover = actualObj.get("cover");
                            Map<String,String> myMap = mapper.readValue(cover.toString(), HashMap.class);
                            Log.e("Cover link", myMap.get("source"));
                            coverpic = myMap.get("source");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "cover");
        request.setParameters(parameters);
        request.executeAsync();

Upvotes: 0

Begum
Begum

Reputation: 675

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.

Upvotes: 1

Spenciefy
Spenciefy

Reputation: 892

I use username instead of id and it works:

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

Parse the JSON accordingly.

Upvotes: 1

Jorge Mora P&#233;rez
Jorge Mora P&#233;rez

Reputation: 11

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;

Upvotes: 1

Zen Nguyễn
Zen Nguyễn

Reputation: 77

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. :(

Upvotes: 1

Omri
Omri

Reputation: 723

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

Upvotes: 25

Priyadarshan Salkar
Priyadarshan Salkar

Reputation: 534

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/

Upvotes: 36

Joe Bobson
Joe Bobson

Reputation: 1386

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

Upvotes: 2

Geeko
Geeko

Reputation: 101

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

Upvotes: 9

Lix
Lix

Reputation: 47986

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.

Upvotes: 1

Related Questions