【问题标题】:Why are the Twitter api calls so slow?为什么 Twitter api 调用这么慢?
【发布时间】:2011-01-14 11:53:30
【问题描述】:

当我执行以下代码时,需要 10-12 秒才能做出响应。

问题出在 Twitter 还是我们的服务器?

我真的需要知道,因为这是在我们的网站上显示推文的代码的一部分,12 秒的加载时间是不可接受的!

function get_latest_tweets($username)
  {
    print "<font color=red>**". time()."**</font><br>";
    $path = 'http://api.twitter.com/1/statuses/user_timeline/' . $username.'.json?include_rts=true&count=2';
    $jason = file_get_contents($path);
    print "<font color=red>**". time()."**</font><br>";
  }

谢谢

【问题讨论】:

  • 我认为很难说为什么特定的网络服务很慢。正如我们所说的 web 服务,所有使 web 变慢的东西也可以应用于 twitter 调用。
  • 我不知道为什么需要这么长时间。但与此同时,您可以使用本地缓存,直到它得到修复。
  • 嗯,如果您只是手动下载该文件,这与您的服务器的 API 请求应该达到的速度大致相同。 (这对于单个请求来说相当快。)如果您在特定时间内有太多请求,服务器可能会限制您的请求。
  • 只需使用一个分析器(如 XDebug),它可以列出单个函数/方法调用的执行时间。如果这不能提供足够的洞察力,请使用 Wireshark 或其他网络嗅探工具来查看这是否是延迟问题(可能)。

标签: php performance twitter


【解决方案1】:

当您将 URL 放入浏览器 (http://api.twitter.com/1/statuses/user_timeline/username.json?include_rts=true&count=2) 需要多长时间页面出现?如果它很快,那么您需要在您的服务器上开始搜索。

【讨论】:

  • 嗨,马特,好电话。通过浏览器完成时速度很快,所以必须是别的东西。任何想法从哪里开始寻找?谢谢
  • 我想和 serverfault.com(stackoverflow 姊妹网站)的一些服务器专家谈谈。他在那里的一篇文章可以帮助你serverfault.com/questions/50273/…
【解决方案2】:

使用 curl 而不是 file_get_contents() 来请求,这样响应就会被压缩。这是我正在使用的 curl 函数。

function curl_file_get_contents($url)
{
    $curl = curl_init();

    curl_setopt($curl,CURLOPT_URL,$url); //The URL to fetch. This can also be set when initializing a session with curl_init().
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
    curl_setopt($curl,CURLOPT_ENCODING , "gzip");

    curl_setopt($curl, CURLOPT_FAILONERROR, TRUE); //To fail silently if the HTTP code returned is greater than or equal to 400.
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);

    $contents = curl_exec($curl);
    curl_close($curl);

    return $contents;
}

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    相关资源
    最近更新 更多