【问题标题】:PHP cURL return empty header and body despite HTTP Code 200尽管 HTTP 代码 200,PHP cURL 返回空的标头和正文
【发布时间】:2015-05-12 11:35:26
【问题描述】:

所以我尝试用 cURL 废弃这个 URL:xxxx.fr,但无法访问页面 HTML 代码,标题和正文都是空的。 HTTP 代码返回 200 我尝试使用其他 URL(不同的域),它就像一个魅力。 我也尝试使用不同的 User Agent 和 Referer

你知道什么是错的吗?至少有人可以在您自己的服务器上尝试此代码并让我知道您是否有同样的问题?

谢谢

下面是我的代码:

  $url = 'http://www.xxxx.fr';

  $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
  $header[] = "Cache-Control: max-age=0";
  $header[] = "Connection: keep-alive";
  $header[] = "Keep-Alive: timeout=5, max=100";
  $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $header[] = "Accept-Language: en-us,en;q=0.5";
  $header[] = ""; // BROWSERS USUALLY LEAVE BLANK

  $curl = curl_init ();
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
  curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0");
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
  curl_setopt($curl, CURLOPT_REFERER, "http://www.google.fr");
  curl_setopt($curl, CURLOPT_HEADER, 1);
  curl_setopt($curl, CURLINFO_HEADER_OUT, 1);
  curl_setopt($curl, CURLOPT_VERBOSE, 1);
  curl_setopt($curl, CURLOPT_COOKIEFILE, getcwd().'/cookies.txt');
  curl_setopt($curl, CURLOPT_COOKIEJAR, getcwd().'/cookies.txt');
  curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  $curlData = curl_exec($curl);

  $infos = curl_getinfo($curl);
  print_r($infos);

  curl_close ( $curl );

  echo "<hr>Page:<br />";
  echo htmlentities($curlData);

这是 print_r($infos) 的结果:

Array ( 
[url] => http://www.xxxx.fr 
[content_type] => text/html 
[http_code] => 200 
[header_size] => 625 
[request_size] => 465 
[filetime] => -1 
[ssl_verify_result] => 0
[redirect_count] => 0 
[total_time] => 0.032535 
[namelookup_time] => 0.001488 
[connect_time] => 0.002581 
[pretransfer_time] => 0.002639 
[size_upload] => 0 
[size_download] => 10234 
[speed_download] => 314553 
[speed_upload] => 0 
[download_content_length] => -1 
[upload_content_length] => 0 
[starttransfer_time] => 0.032088 
[redirect_time] => 0 
[certinfo] => Array ( ) 
[primary_ip] => xxx 
[primary_port] => 80 
[local_ip] => xxx 
[local_port] => 37319 
[redirect_url] => 
[request_header] => GET / HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0 Host: www.xxxx.fr Accept-Encoding: gzip,deflate Referer: http://www.google.fr Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 Cache-Control: max-age=0 Connection: keep-alive Keep-Alive: timeout=5, max=100 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Accept-Language: en-us,en;q=0.5 
) 

【问题讨论】:

  • 所以$curlData 对你来说是空的?当我执行你的代码时,它会为我返回 HTML。
  • 当我在我的服务器上尝试 curl -v 命令时,我可以获得 HTML 代码,所以我认为我的 PHP / cURL 配置中存在阻塞,但不确定是什么....
  • 实际上,如果我执行 var_dump($curlData),它会打印 HTML 代码,否则它是空的。好奇怪……

标签: php string http curl


【解决方案1】:

//编辑

htmlentities($curlData) 返回空字符串,因为源的编码是非 UTF-8 字符串see this link

应该可以:

 htmlentities($curlData, ENT_QUOTES,'ISO-8859-1' );

在 PHP 5.4 版本中,htmlspecialchars() 不使用 ISO-8859-1 作为默认编码。事实上 htmlspecialchars() 自 PHP 5.4 起使用 UTF-8。您可能期望,htmlspecialchars() 只会跳过非 UTF-8 字节序列或将它们转换为“未找到”字符。实际上,htmlspecialchars() 返回一个空白字符串:没有错误生成,没有错误代码返回,没有异常被引发,如果传入了无效的 UTF-8 序列,则只返回一个空白字符串

【讨论】:

  • 这不是答案。 $curlData 已填充。也是一个只有“使用”的“答案”,而且非常无益。 OP 对代码在做什么或为什么要使用它一无所知。
  • 您是否按原样运行代码?有用。 htmlentities() 不是问题,它只会将代码带回实体化; OP 状态不为空是问题both header and body are empty。返回片段:&amp;lt;/script&amp;gt; &amp;lt;/body&amp;gt; &amp;lt;/html&amp;gt;
  • 当我运行他的代码和 var_dump(htmlentities($curlData)); return string(0)"" 所以我认为这是问题
  • 我返回 string(49035) "HTTP/1.1 200 OK 和 htmlentities 和 string(37714) "HTTP/1.1 200 OK 和 html_entity_decode。其中没有一个是空的,所以我认为函数不是问题。
  • @chris85 我更新了我的答案,我假设您使用与源代码相同的字符集测试他的代码,当对脚本和源代码使用不同的编码时会出现问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-04
  • 2017-10-30
  • 1970-01-01
  • 2019-06-14
  • 2017-11-25
  • 2013-08-25
  • 2011-09-30
相关资源
最近更新 更多