在php中使用curl获取Content-Length页面

时间:2015-05-06 06:16:31

标签: php curl

我正在尝试使用以下代码获取页面的Content-Lengtt,但它仅显示var input = 'Enter 🐦 into the field.'; var decoded = he.decode(input); console.log(decoded); // → 'Enter into the field.' 作为Content-Length

var escaped = cssesc(decoded);
console.log(escaped);
// → 'Enter \1F426  into the field.'

我正在

float(-1)

我将$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-Length

1 个答案:

答案 0 :(得分: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;
  }