Php 为什么twitterapi调用如此缓慢?

Php 为什么twitterapi调用如此缓慢?,php,performance,twitter,Php,Performance,Twitter,当我执行以下代码时,需要10-12秒来响应 是Twitter的问题还是我们的服务器的问题 我真的需要知道,因为这是在我们的网站上显示推文代码的一部分,12秒的加载时间是不可接受的 function get_latest_tweets($username) { print "<font color=red>**". time()."**</font><br>"; $path = 'http://api.twitter.com/1/status

当我执行以下代码时,需要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>";
  }
函数获取最新推文($username)
{
打印“**.”time().“**
”; $path=http://api.twitter.com/1/statuses/user_timeline/'.$username.'.json?包括_rts=true&count=2'; $jason=文件获取内容($path); 打印“**.”time().“**
”; }

当您将URL放入浏览器时,谢谢(http://api.twitter.com/1/statuses/user_timeline/username.json?include_rts=true&count=2)页面显示需要多长时间?如果搜索速度快,则需要在服务器上开始搜索。

使用curl而不是file\u get\u contents()来请求,这样响应将被压缩。这是我使用的旋度函数

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;
}

我认为很难说为什么一个特定的Web服务速度慢。当我们谈论web服务时,所有使web变慢的因素也可以应用于twitter呼叫。我不知道为什么需要这么长时间。但与此同时,你可以使用本地缓存,直到它得到修复。嗯,如果你只是手动下载该文件,这与你服务器的API请求应该得到的速度差不多。(对于单个请求来说,这相当快。)如果您在特定时间内有太多的请求,服务器可能会限制您的请求。只需使用可以列出单个函数/方法调用的执行时间的探查器(如XDebug)。如果这还不能提供足够的洞察力,请使用Wireshark或其他网络嗅探工具查看这是否是延迟问题(可能)。嗨,Matt,打得好。当通过浏览器完成时,速度很快,所以一定是其他东西。你知道从哪里开始找吗?ThanksI将与serverfault.com(一个stackoverflow姐妹网站)上的一些服务器专家谈谈。他在那里有一个职位可能对你有帮助