我使用PHP脚本从粉丝页面获取所有帖子。代码是这样的:
require_once("../facebook-sdk/src/facebook.php");
$config = array(
'appId' => '#############',
'secret' => '###############################',
'fileUpload' => false
);
$facebook = new Facebook($config);
$facebook->setAccessToken("###################################");
$pageid = "###############";
$pagefeed = $facebook->api("/" . $pageid . "/feed");
$i = 0;
foreach($pagefeed['data'] as $post) {
if ($post['type'] == 'video' || $post['type'] == 'link' || $post['type'] == 'photo') {
// open up an fb-update div
echo "<div class=\"fb-update\">";
// check if post type is a link
if ($post['type'] == 'link') {
echo '<a href="' . $post['link'] . '" target="_blank"><img class="imagem-feed" src="' . $post["picture"] . '"></a>';
$interno = "<p>" . $post['message'] . "</p><p><a href=\"" . $post['link'] . "\" target=\"_blank\">" . $post['link'] . "</a></p>";
}
// check if post type is a photo
if ($post['type'] == 'photo') {
$fotoAlta = $facebook->api("/" . $post["object_id"] . "?fields=source");
echo '<a href="' . $post['link'] . '" target="_blank"><img class="imagem-feed" src="' . $fotoAlta["source"] . '"></a>';
//interno
if (empty($post['story']) === false) {
$interno = "<p>" . $post['story'] . "</p>";
} elseif (empty($post['message']) === false) {
$interno = "<p>" . $post['message'] . "</p>";
}
$interno .= "<p><a href=\"" . $post['link'] . "\" target=\"_blank\">Ver no Facebook →</a></p>";
}
// check if post type is a video
if ($post['type'] == 'video') {
echo '<iframe class="imagem-feed" width="350" height="263" src="' . str_replace("&autoplay=1","",$post["source"]) . '" frameborder="0" allowfullscreen></iframe>';
//interno
if (empty($post['story']) === false) {
$interno = "<p>" . $post['story'] . "</p>";
} elseif (empty($post['message']) === false) {
$interno = "<p>" . $post['message'] . "</p>";
}
}
echo '<div class="cabecalho-fanpage"><a target="_blank" href="https://www.facebook.com/Angelameza.arquitetura"><img class="img-perfil-fanpage" width="50" height="50" src="http://profile.ak.fbcdn.net/hprofile-ak-ash1/373040_201176906637605_665931623_q.jpg"><h1>' . $post["from"]["name"] . '</h1><p>' . date("d/m/Y", (strtotime($post['created_time']))) . '</p></a></div>';
echo $interno;
echo '<div class="container-interacoes">';
$totalCurtidas = $facebook->api("/" . $post["id"] . "/likes/?summary=true");
$titulo = ($totalCurtidas["summary"]["total_count"] == 0 || $totalCurtidas["summary"]["total_count"] > 1) ? $totalCurtidas["summary"]["total_count"] . " pessoas curtiram isso." : "1 pessoa curtiu isso.";
echo "<span title='$titulo' class='icon-curtidas'>" . $totalCurtidas["summary"]["total_count"] . "</span>";
$compartilhamentos = (isset($post["shares"]["count"])) ? $post["shares"]["count"] : 0;
$titulo = ($compartilhamentos == 0 || $compartilhamentos > 1) ? $compartilhamentos . " pessoas compartilharam isso." : "1 pessoa compartilhou isso.";
echo "<span title='$titulo' class='icon-compartilhamentos'>" . $compartilhamentos . "</span>";
$totalComentarios = $facebook->api("/" . $post["id"] . "/comments/?summary=true");
$titulo = ($totalComentarios["summary"]["total_count"] == 0 || $totalComentarios["summary"]["total_count"] > 1) ? $totalComentarios["summary"]["total_count"] . " pessoas comentaram isso." : "1 pessoa comentou isso.";
echo "<span title='$titulo' class='icon-comentarios'>" . $totalComentarios["summary"]["total_count"] . "</span>";
echo "</div>";
echo "<div style='clear:both'></div></div>"; // close fb-update div
$i++; // add 1 to the counter if our condition for $post['type'] is met
}
} // end the foreach statement
使用此代码,页面非常慢(加载50秒)。我测试了任何优化的东西而且没有改进。有人能帮助我吗?
答案 0 :(得分:0)
您正在for循环中调用图形API,即对于每个帖子,您正在进行其他调用。显然,这有助于你的50秒。最好的办法是安排代码使用批量请求。这是批量调用的文档。
https://developers.facebook.com/docs/graph-api/making-multiple-requests/
注意:您可以使用批量请求一次拨打最多50个电话。