Why are the Twitter api calls so slow?

2019-08-06 04:53发布

When I execute the following code it takes between 10-12 seconds to respond.

Is the problem with Twitter or with our server?

I really need to know as this is part of the code to display tweets on our website and a 12 second load time is just not acceptable!

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

Thanks

2条回答
劳资没心,怎么记你
2楼-- · 2019-08-06 05:27

use curl instead of file_get_contents() to request, so that response will be compressed. Here is the curl function which iam using.

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;
}
查看更多
可以哭但决不认输i
3楼-- · 2019-08-06 05:46

When you put the URL into your browser (http://api.twitter.com/1/statuses/user_timeline/username.json?include_rts=true&count=2) how long does it take for the page to appear? If it's quick then you need to start the search at your server.

查看更多
登录 后发表回答