繁体   English   中英

php curl CURLOPT_HEADER 和 DOM

[英]php curl CURLOPT_HEADER and DOM

我有以下代码:

curl_setopt($ch, CURLOPT_URL, $host);
  curl_setopt($ch, CURLOPT_HEADER, 1); 
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  $html = curl_exec($ch);


  preg_match_all('|Set-Cookie: (.*);|U', $html, $results);  
  $cookies = implode(';', $results[1]);


  $dom = new DOMDocument();
  $dom->loadHTML($html);

在线$dom->loadHTML($html); 我收到以下错误:

Warning: DOMDocument::loadHTML() [function.DOMDocument-loadHTML]:
Misplaced DOCTYPE declaration in
Entity, line: 12 in
D:\Programs\xampp\xampp\htdocs\ip\megafonmoscow.php
on line 39

Warning: DOMDocument::loadHTML()
[function.DOMDocument-loadHTML]:
htmlParseStartTag: misplaced 
tag in Entity, line: 13 in
D:\Programs\xampp\xampp\htdocs\ip\megafonmoscow.php
on line 39

Warning: DOMDocument::loadHTML()
[function.DOMDocument-loadHTML]:
htmlParseStartTag: misplaced 
tag in Entity, line: 14 in
D:\Programs\xampp\xampp\htdocs\ip\megafonmoscow.php
on line 39

Warning: DOMDocument::loadHTML()
[function.DOMDocument-loadHTML]:
Unexpected end tag : head in Entity,
line: 32 in
D:\Programs\xampp\xampp\htdocs\ip\megafonmoscow.php
on line 39

Warning: DOMDocument::loadHTML()
[function.DOMDocument-loadHTML]:
htmlParseStartTag: misplaced 
tag in Entity, line: 34 in
D:\Programs\xampp\xampp\htdocs\ip\megafonmoscow.php
on line 39

是线curl_setopt($ch, CURLOPT_HEADER, 1); 这个错误的原因? 因为饼干,我需要它。 关于如何解决这个问题的任何想法?

mck89 方法的替代方法是将标题和正文下载在一起,但在尝试解析它之前将它们拆分:

$html = curl_exec($ch);

[snip]

$html = preg_replace('/^.*\n\n/s','',$html,1); // strip out everything before & including the double line break between headers and body

$dom = new DOMDocument();
$dom->loadHTML($html);

这节省了 HTTP 请求,因此节省了一定的时间。

尝试删除该行,以便它不会返回标头,然后在 curl 请求之后使用get_headers函数获取它们。

  curl_setopt($ch, CURLOPT_URL, $host);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  $html = curl_exec($ch);
  $headers=get_headers($host, 1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM