我试图从一个facebook粉丝页面在我的网站上显示一些自定义facebook feed。这是我使用的php的总结示例,它工作得很好。
[...html code...]
// include the facebook sdk
require_once('resources/facebook-php-sdk-master/src/facebook.php');
// connect to app
$config = array();
$config['appId'] = 'MY_APP_ID';
$config['secret'] = 'MY_SECRET_CODE';
$config['fileUpload'] = false; // optional
// instantiate
$facebook = new Facebook($config);
// set page id
$pageid = "MY_PAGE_ID";
// access to the graph, starting with the feed
$pagefeed = $facebook->api("/" . $pageid . "/feed");
[...html code...]
$i = 0;
foreach($pagefeed['data'] as $post) {
// check if post type is a photo and catch the various part of the graph
if ($post['type'] == 'photo') {
//grab the thumbnail url in the graph
$picture_url = $post['picture'];
//get true sized photo by manipulating its url
$picture_url_big = str_replace("s130x130/","", $picture_url);
echo "<p><img class='"img-icon'" src='"" . $post['icon'] . "'"></p>";
echo "<h2 class='"data-post'">" . date("j-n-Y", (strtotime($post['created_time']))) . "</h2>";
//displaying the photo
echo "<div class='"img-thumb'"><a href='"" . $post['link'] . "'" target='"_blank'"><img src='"" . $picture_url_big . "'"></div></a>";
echo "<p class='"manda-a-capo'"></p>";
if (empty($post['story']) === false) {
echo "<p>" . $post['story'] . "</p>";
} elseif (empty($post['message']) === false) {
echo "<p>" . $post['message'] . "</p>";
}
echo "<p><a href='"" . $post['link'] . "'" target='"_blank'"><u><b>Vedi foto</b></u></a></p>";
echo "<p class='"manda-a-capo'"></p>";
if ($post['shares']['count'] != "") {
echo "<p class='"manda-a-capo share-num'">" . $post['shares']['count'] . " condivisioni.</p>";
}
}
$i++;
}
[...other code...]
facebook图只包含照片的拇指url,即130x130px。我发现有些拇指在url中有一个"/s130x130/"参数,如果删除这个参数,就会得到实际大小的照片。这就解释了这部分代码(如上所述):
//grab the thumbnail url in the graph
$picture_url = $post['picture'];
//get true sized photo by manipulating its url
$picture_url_big = str_replace("s130x130/","", $picture_url);
//then displaying the photo
echo "<div class='"img-thumb'"><a href='"" . $post['link'] . "'" target='"_blank'"><img src='"" . $picture_url_big . "'"></div></a>";
不幸的是,我注意到并非所有的照片从页面有这个参数,其中一些甚至有一个不同的url结构。所以最终的结果是,我只能到达少数照片的实际大小,其他的仍然是一个破碎的链接。
是否有一种方法来操纵url,以获得所有的照片在自己的实际大小?任何建议吗?
谢谢。
注:下面是查看fb图表的php代码:
<?php
echo "<pre>";
print_r($pagefeed);
echo "</pre>";
?>
我找到了一个临时解决方案。为了显示缺失的链接,我添加了一个php函数来检查图像url是否存在。
function checkRemoteFile($picture_url_big)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$picture_url_big);
// don't download content
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
return true;
}
else
{
return false;
}
}
和echo
上方的控件if (checkRemoteFile($picture_url_big)) {
//echo "image exist ";
$check = true;
$picture_url_big;
} else {
//echo "image does not exist ";
$check = false;
$picture_url_big = $picture_url;
}