php - Optimize connection with Facebook -


i'm using script php posts fan page. code this:

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'] $post) {     if ($post['type'] == 'video' || $post['type'] == 'link' || $post['type'] == 'photo') {          // open fb-update div         echo "<div class=\"fb-update\">";          // check if post type 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 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 &rarr;</a></p>";         }          // check if post type 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 counter if our condition $post['type'] met     } } // end foreach statement 

using code, page slow (50 seconds load). tested thing optimize , don't improve. can me?

you making calls graph api inside loop, i.e every post, making additional calls. obviously, contributes towards 50 secs. best bet arrange code use batch requests. here documentation batching calls.

https://developers.facebook.com/docs/graph-api/making-multiple-requests/

note: can make upto 50 calls in 1 go using batch request.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -