PHP 函数curl_exec() 减慢了我的脚本速度

发布于 2024-09-26 11:36:05 字数 1431 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心↖ 2024-10-03 11:36:05

我刚刚在我的机器上测试了您的代码(仅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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文