如何加快php中的cURL? [英] How to speed up cURL in php?

查看:325
本文介绍了如何加快php中的cURL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Twitter嵌入推文.因此,我正在使用cURL取回json.我写了一个小测试,但是测试和我在本地运行大约需要5秒钟.所以,我不确定在这里我在做什么错.

I'm trying to get embed tweet from Twitter. So, I'm using cURL to get the json back. I wrote a little test but the test takes around 5 seconds as well as when I run locally. So, I'm not sure what am I doing wrong here.

public function get_tweet_embed($tw_id) {

    $json_url = "https://api.twitter.com/1/statuses/oembed.json?id={$tw_id}&align=left&omit_script=true&hide_media=false";

    $ch = curl_init( $json_url );
    $start_time = microtime(TRUE);
    $JSON = curl_exec($ch);
    $end_time = microtime(TRUE);
    echo $end_time - $start_time; //5.7961111068726

    return $this->get_html($JSON);
}

private function get_html($embed_json) {
    $JSON_Data = json_decode($embed_json,true);
    $tw_embed_code = $JSON_Data["html"];
    return $tw_embed_code;
}

当我粘贴链接并通过浏览器对其进行测试时,它的速度确实非常快.

When I paste the link and test it from the browser it's really fast.

推荐答案

关于环境,我已经在PHP中观察到cURL在大多数环境中通常运行非常快,除非在CPU较低且网络速度较慢的地方表现.例如,在我的MAMP安装上的localhost上,curl快速,在一个较大的亚马逊实例上,curl快速.但是在小型的托管主机上,我发现它存在性能问题,连接速度明显较慢.不过,我不确定为什么为什么慢一些.而且,它肯定不会慢5秒.

With respect to environment, I've observed in PHP that cURL typically runs very fast in most environments except in places where there is low CPU and there is slower network performance. For example, on localhost on my MAMP installation, curl is fast, on a larger amazon instance, curl is fast. But on a small crappy hosting, i've seen it have performance issues where it is noticeably slower to connect. Though, i'm not sure exactly why that is slower. Also, it sure wasn't 5 seconds slower.

为了帮助确定它是PHP还是您的环境,您应该尝试通过命令行与curl进行交互.至少您可以将PHP代码排除在问题之外(如果仍然需要5秒钟).

to help determine if its PHP or your environment, you should try interacting with curl via the command line. At least that you'll be able to rule out PHP code being the problem if its still 5 seconds.

这篇关于如何加快php中的cURL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆