php - 使用 PHP 的 CURL - 非常慢

标签 php curl libcurl

全部,

我必须请求一个返回 JSON 请求的 URL。我正在使用 PHP 和 CURL 来执行此操作。目前,请求和响应大约需要 3-4 秒。

curl代码如下

    $ch = curl_init();
    $devnull = fopen('/tmp/curlcookie.txt', 'w');

    curl_setopt($ch, CURLOPT_STDERR, $devnull);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $desturl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);

    $ret = curl_exec($ch);

    curl_close($ch);

    if ($devnull)
    {
        fclose($devnull);
    }

CURL_GETINFO 数组如下

Array
(
    [url] => https://xx.xx.xxx.xx/portalsite/tester
    [content_type] => application/json
    [http_code] => 200
    [header_size] => 198
    [request_size] => 835
    [filetime] => -1
    [ssl_verify_result] => 20
    [redirect_count] => 0
    [total_time] => 2.054561
    [namelookup_time] => 6.5E-5
    [connect_time] => 0.016048
    [pretransfer_time] => 0.123947
    [size_upload] => 699
    [size_download] => 46735
    [speed_download] => 22746
    [speed_upload] => 340
    [download_content_length] => 0
    [upload_content_length] => 0
    [starttransfer_time] => 1.743973
    [redirect_time] => 0
)

如何加快 CURL 处理时间?

谢谢

最佳答案

根据 this answer (类似问题)如果您在 Mac OS X 上并且使用 xxxx.local 访问您的项目(使用 127.0.0.1 myproject.local 在您的 /etc/hosts/

正如@lepix 所说:

It is because the .local tld is reserved for Bonjour service, and this since Mac OS X Lion (10.7).

感谢 lepix,希望它能有所帮助。

关于php - 使用 PHP 的 CURL - 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1976824/

相关文章:

php - set_exception_handler()期望参数(exception_handler)是有效的回调

c++ - 如何让 CMAKE 在 OSX 上找到特定版本的 lib curl?

http - CURL 命令不适用于简单的 HTTP GET 但浏览器可以

c - 在 C 中使用 libcurl 和 liboauth 发布推文

c - libcurl c/c++ Kerberos 身份验证

php - MySQL 的 Json 菜单结构

php - Base64解码返回空白

php - 从特定 mysql 表列检索数据值并在 php 中打印

bash - 使用 bash <(curl ...) 转义 Makefile 规则中的括号

c - 如何取消 libCurl 请求的示例