Getting Facebooks' Cover Photo via PHP SDK & G

2019-02-04 20:17发布

问题:

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.

Thanks in advance.

回答1:

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/



回答2:

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


回答3:

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


回答4:

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


回答5:

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


回答6:

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.



回答7:

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



回答8:

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;


回答9:

I use username instead of id and it works:

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

Parse the JSON accordingly.



回答10:

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.



回答11:

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();


回答12:

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

    });
}


回答13:

Here's the link to fetch facebook cover pic..

http://www.77-thoughts.com/fetch-facebook-cover-pic-using-facebook-graph-api/

You can demo it here

http://77-thoughts.com/demo/fetch_facebook_cover_picture_graph_api_demo.html