简体   繁体   中英

PHP Facebook Graph API and full or larger size post images

Did some searching but nothing is coming up or is old and doesn't work

I'm using the graph api to get facebook posts and formatting them on my site, the URL/curl/json thing which is working; however, the picture returned defaults to the smallest 130x130 version. I added in a second json inside the foreach loop for each post to grab the larger image URL but this slows down the page load significantly.

Is there a parameter for the posts that will grab the larger 720x720 image URl instead of the tiny one pulled by default. I tried the fields=full_picture used for the second (in loop) one but it doesn't work.

Also tried using the file_get_contents() function instead of the cURL solution but there was no noticeable speed difference

I also tried just doing a string replace to the picture data of the initial array but facebook adds those oh= and oe= tokens to their images URLs now so that trick doesn't work either.

$limit = 20;
//App Info, needed for Auth
$app_id = "app id removed :)";
$app_secret = "seceret code removed :)";

$url = 'https://graph.facebook.com/'.$profile_id.'/posts?limit='.$limit.'&access_token='.$app_id.'|'.$app_secret;
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
$json = json_decode($data, true);

foreach($json[data] as $child) {
    $thisDate = $child['created_time'];
    $url='https://graph.facebook.com/' . $child['id'] . '?fields=full_picture&access_token='.$app_id.'|'.$app_secret;
    /*
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    $pic = json_decode($data, true);
    */
    $pic = json_decode(file_get_contents($url),true);
}

curl_close($ch);

This solution does work and provides the larger images but just slows down the page load due to the extra cURL call inside the loop.

Thank you

You can get that data in one request, by using the fields parameter:

/me/posts?fields=message,full_picture

(If you want more additional fields, you have to list them as well.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM