簡體   English   中英

在PHP中使用curl獲取頁面的內容長度

[英]get Content-Length of page using curl in php

我正在嘗試使用以下代碼獲取頁面的Content-Lengtt,但它僅將float(-1)為Content-Length

$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作為float(-1)我需要得到Content-Length: 347這樣的

這是肯定的方法

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;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM