【问题标题】:Websites using the Facebook graph api are loading very slow使用 Facebook graph api 的网站加载速度非常慢
【发布时间】:2019-05-17 16:44:12
【问题描述】:

我有一些网站有一个自定义 Facebook 提要,我使用 Graph API 加载这些提要。

过去,所有内容的加载速度都非常快,但大约一两周后,所有网站都会在提要部分停止加载,并且仅在 30 秒或更长时间后才完全加载。

这可能是什么原因?

我确定它是 Facebook 提要,因为当我删除它时,网站会再次快速加载。

以下是来自存在此问题的网站的示例提要:

$json_object = file_get_contents("https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken");

$feedarray = json_decode($json_object);

$f = 0;

foreach ( $feedarray->data as $key => $feed_data )
{

if($feed_data->full_picture != ''){
  $fbimage = $feed_data->full_picture;
}else{
  $fbimage = 'assets/images/fbnoimg.jpg';
}

$shortstrfb = substr($feed_data->message, 0, 170) . '...';
if($feed_data->message != ''){
  $f++;
}

if($f > 4){
  break;
}

if($feed_data->message != '' && $feed_data->from->name == 'facebook page name'){
      $facebookfeed .= '

      <div class="col-lg-3 col-md-4 col-sm-6">
          <div class="single-product-wrap fbwrapdiv">
              <div class="product-image">
                  <a href="'.$feed_data->permalink_url.'" target="_blank">
                  <span class="datefb">'.date("d-m-Y",strtotime($feed_data->updated_time)).'</span>
                  <img class="fbimgclass" src="'.$fbimage.'" alt=""></a>
                  <div class="product-action">
                      <a href="'.$feed_data->permalink_url.'" target="_blank" class="wishlist"><i class="fab fa-facebook-f"></i></a>
                  </div>
              </div>
              <div class="product-content">
                  <div class="price-box">
                      <p class="facebooktext">'.$shortstrfb.'</p>
                  </div>
              </div>
          </div>
      </div>';
    }
}
echo $facebookfeed;

【问题讨论】:

  • 在每次页面加载时发出新的 API 请求并不是一件聪明的事情。你应该以某种方式缓存这些数据。
  • 另外,我会使用 curl 而不是 file_get_contents。
  • @luschn 谢谢,这为我解决了问题。现在一切都再次快速加载。

标签: facebook facebook-graph-api


【解决方案1】:

我使用 curl 修复了它。

我换了:

$json_object = file_get_contents("https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken");

$feedarray = json_decode($json_object);

与:

$graph_url = 'https://graph.facebook.com/v3.2/thepagesid/posts?fields=full_picture%2Cmessage%2Cstory%2Cpermalink_url%2Cupdated_time%2Cfrom&access_token=mytoken';
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $graph_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$output = curl_exec($ch);

curl_close($ch);

$feedarray = json_decode($output);

【讨论】:

  • 这只是速度的优化,但你真的需要缓存它而不是像 04FS 建议的那样在每个页面上调用 api。即使使用 curl,API 调用也需要时间,并且存在 api 限制。
猜你喜欢
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
  • 2016-06-11
  • 1970-01-01
  • 2017-07-02
  • 2011-11-23
  • 2012-03-07
  • 2017-01-04
相关资源
最近更新 更多