1

我绞尽脑汁想弄清楚为什么在使用 curl 获取同一域上的页面内容时我的启动传输时间如此之长。

http://dragon.pbndev.net/?rah_external_output=team_pages。在我的浏览器中获取此页面的响应为 ~200 毫秒,相同的页面在 curl 我的 starttransfer_time 为 ~16 秒。

如果我将下面的内容切换为https://www.google.com,我可以获得非常快的响应,就像我在浏览器中点击目标网址一样。

我从这里的其他“我的 curl 请求不起作用”问题中获得了很多这些选项。我只是想不通为什么响应时间这么长。

我也尝试了上面相同的网址,file_get_contents()但我得到了一个空的回复。

$ch = curl_init();
$getTemplateFromDomain = 'http://dragon.pbndev.net/?rah_external_output=team_pages';
curl_setopt($ch, CURLOPT_URL, $getTemplateFromDomain);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: close'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$response = curl_exec($ch);
$errStr = curl_error($ch);
$errNum = curl_errno($ch);
$head = curl_getinfo($ch, CURLINFO_HEADER_OUT);
$ci = curl_getinfo($ch);
var_dump(array('head' => $head, 'error_string' => $errStr, 'error_number' =>  $errNum, 'curl_info' =>  $ci, 'response' => $response));

这是我从 var_dump 得到的响应,所以我可以得到内容,只是需要很长时间。

array (size=5)
    'head' => boolean false
    'error_string' => string '' (length=0)
    'error_number' => int 0
    'curl_info' => 
        array (size=22)
        'url' => string 'http://dragon.pbndev.net/?rah_external_output=team_pages' (length=56)
        'content_type' => string 'text/plain; charset=utf-8' (length=25)
        'http_code' => int 200
        'header_size' => int 562
        'request_size' => int 222
        'filetime' => int -1
        'ssl_verify_result' => int 0
        'redirect_count' => int 0
        'total_time' => float 16.195335
        'namelookup_time' => float 0.001552
        'connect_time' => float 0.001802
        'pretransfer_time' => float 0.002
        'size_upload' => float 0
        'size_download' => float 7007
        'speed_download' => float 432
        'speed_upload' => float 0
        'download_content_length' => float -1
        'upload_content_length' => float 0
        'starttransfer_time' => float 16.19518
        'redirect_time' => float 0
        'certinfo' => 
             array (size=0)
                 empty
        'redirect_url' => string '' (length=0)
        'response' => string '<!doctype html>
            <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
            <!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
            <!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
            <!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
            <head>
                <meta charset="utf-8">

                <meta name="description" content="Lake Bemidji Dragon Boat Festival's website">
                <meta name="keywords" content="Bemidji, Minne'... (length=7007)

最终,我会将结果缓存在 memcache 中,这样我就不必花太多时间了,但我想首先弄清楚为什么要花这么长时间。

4

0 回答 0