Getting Facebooks' Cover Photo via PHP SDK & G

2019-02-04 19:55发布

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.

13条回答
贪生不怕死
2楼-- · 2019-02-04 20:26

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楼-- · 2019-02-04 20:26

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();
查看更多
一纸荒年 Trace。
4楼-- · 2019-02-04 20:30

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.

查看更多
smile是对你的礼貌
5楼-- · 2019-02-04 20:33

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

查看更多
成全新的幸福
6楼-- · 2019-02-04 20:34

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;
    }
}
查看更多
登录 后发表回答