PHP 函数curl_exec() 减慢了我的脚本速度
我在 CodeIgniter 中使用以下函数来获取我的最新推文:
function tweet($id) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/".$id.".xml?count=1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
//Benchmark starts here
$src = curl_exec($c);
//Benchmark ends here
curl_close($c);
preg_match('/<text>(.*)<\/text>/', $src, $t);
$data['tweet'] = htmlentities($t[1]);
preg_match('/<created_at>(.*)<\/created_at>/', $src, $c);
$created = $c[1];
// explode $created so we can process it
$created_array = explode(' ',$created);
$time = $created_array[3];
$time_array = explode(':',$time);
$format = '%b/%d/%Y %H:%M';
$date_to_format = $created_array[1].'/'.$created_array[2].'/'.$created_array[5].' '.$time_array[0].':'.$time_array[1];
$date_time = strptime($date_to_format,$format);
$created_timestamp = mktime($date_time['tm_hour'], $date_time['tm_min'], 0, $date_time['tm_mon']+1, $date_time['tm_mday'], $date_time['tm_year']+1900);
$time_diff = time() - $created_timestamp;
$data['time'] = time_since($time_diff);
return $data;
}
我使用 CI 的 Benchmark 类来查看为什么网站需要这么长时间才能响应,我发现该行
$src = curl_exec($c);
需要超过 5 秒的时间来执行。谁能告诉我为什么会发生这种情况?
I use the following function in CodeIgniter, to get my latest tweet:
function tweet($id) {
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/".$id.".xml?count=1");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
//Benchmark starts here
$src = curl_exec($c);
//Benchmark ends here
curl_close($c);
preg_match('/<text>(.*)<\/text>/', $src, $t);
$data['tweet'] = htmlentities($t[1]);
preg_match('/<created_at>(.*)<\/created_at>/', $src, $c);
$created = $c[1];
// explode $created so we can process it
$created_array = explode(' ',$created);
$time = $created_array[3];
$time_array = explode(':',$time);
$format = '%b/%d/%Y %H:%M';
$date_to_format = $created_array[1].'/'.$created_array[2].'/'.$created_array[5].' '.$time_array[0].':'.$time_array[1];
$date_time = strptime($date_to_format,$format);
$created_timestamp = mktime($date_time['tm_hour'], $date_time['tm_min'], 0, $date_time['tm_mon']+1, $date_time['tm_mday'], $date_time['tm_year']+1900);
$time_diff = time() - $created_timestamp;
$data['time'] = time_since($time_diff);
return $data;
}
I use the Benchmark class of CI to see why the website takes so long to respond, and I found that the line
$src = curl_exec($c);
takes more than 5 seconds to execute. Can anyone tell me why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 file_get_contents 执行此操作。
http:// /philsturgeon.co.uk/news/2009/07/如何创建具有完整语法支持的 Twitter-feed
Try just doing this with file_get_contents.
http://philsturgeon.co.uk/news/2009/07/How-to-Create-a-Twitter-feed-with-full-syntax-support
我刚刚在我的机器上测试了您的代码(仅curl请求)并且没有任何问题......它快速检索数据:349ms。
会不会是你的网络有问题?或者也许 Tweeter 在您测试请求时正好有时间?上次我尝试使用 Twitter API 时,整个网站都瘫痪了,所以也许他们也遇到了问题。
祝你好运
I just tested your code on my machine (the curl request only) and there isn't any problem... It retrieves the data quickly: 349ms.
Could it be your network that has issues? Or maybe Tweeter had a moment right when you tested your request? Last time I tried to play with the Twitter API, the whole site was down so maybe they had issues on their side as well.
Good luck