2015-05-06 50 views
-1

我试图让一个页面下面的代码我已经使用的内容Lengtt页的内容长度,但只有float(-1)作为Content-Length的获得在PHP中使用卷曲

$url="https://www.dropbox.com/favicon.ico"; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 

curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1"); 


$content = curl_exec($ch); 

$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$curlInfo = curl_getinfo($ch); 
//echo $content; 
// if (strpos($content,"shortcut icon") !== false) { 
//  echo 'true'; 
// } 
// else 
// echo "no"; 
echo '<pre>'; var_dump($curlInfo); echo '</pre>'; 

我得到它显示

array(26) { 
    ["url"]=> 
    string(35) "https://www.dropbox.com/favicon.ico" 
    ["content_type"]=> 
    NULL 
    ["http_code"]=> 
    int(404) 
    ["header_size"]=> 
    int(0) 
    ["request_size"]=> 
    int(171) 
    ["filetime"]=> 
    int(-1) 
    ["ssl_verify_result"]=> 
    int(20) 
    ["redirect_count"]=> 
    int(0) 
    ["total_time"]=> 
    float(0.812) 
    ["namelookup_time"]=> 
    float(0) 
    ["connect_time"]=> 
    float(0.266) 
    ["pretransfer_time"]=> 
    float(0.812) 
    ["size_upload"]=> 
    float(0) 
    ["size_download"]=> 
    float(0) 
    ["speed_download"]=> 
    float(0) 
    ["speed_upload"]=> 
    float(0) 
    ["download_content_length"]=> 
    float(-1) 
    ["upload_content_length"]=> 
    float(-1) 
    ["starttransfer_time"]=> 
    float(1.092) 
    ["redirect_time"]=> 
    float(0) 
    ["redirect_url"]=> 
    string(0) "" 
    ["primary_ip"]=> 
    string(15) "108.160.172.206" 
    ["certinfo"]=> 
    array(0) { 
    } 
    ["primary_port"]=> 
    int(443) 
    ["local_ip"]=> 
    string(13) "192.168.1.220" 
    ["local_port"]=> 
    int(59397) 
} 

我得到Content-Lengthfloat(-1) 我需要得到Content-Length: 347这样

回答

1

这是肯定的方式

curl_setopt($ch, CURLOPT_ENCODING,""); 


$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
$totalsize = strlen($httpCode); 

$stats = curl_getinfo($ch); 

$size1 = $stats['size_download']; 
    $size2 = $stats['download_content_length']; 

    if ($size1 > 1 && $size1 > $size2){ 
    $downloadSize = $size1; 
    $compression = $totalsize/$size1; 
    } 
    elseif ($size2 > 1){ 
    $downloadSize = $size2; 
    $compression = $totalsize/$size2; 
    } 
    else { 
    $compression = 0; 
    $downloadSize = $totalsize; 
    } 
+0

是什么'$ stats' VAR在这里?更新了 – Thamaraiselvam

+0

,我遇到了同样的问题。第一种方法是肯定的,只要你没有得到gziped的HTML。 – Misunderstood

+0

'$ totalsize' ??也没有定义 – Thamaraiselvam