【问题标题】:PHP file_get_contents returns just newlinesPHP file_get_contents 只返回换行符
【发布时间】:2012-10-07 11:46:40
【问题描述】:

我只有一个用于 HTML 解析的 PHP 脚本,它适用于简单的网站,但现在我需要解析来自 this website 的电影程序。我正在使用file_get_contents 函数,它只返回4 个新行分隔符\n,我就是不知道为什么。 网站本身将更难用 DOMDocument 解析 XPath,因为程序本身只是弹出窗口,它似乎并没有改变 URL 地址,但我会在检索网站的 HTML 代码后尝试处理这个问题.

这是我的脚本的缩短版:

<?php
      $url = "http://www.cinemacity.cz/";
      $content = file_get_contents($url);
      $dom = new DomDocument;
      $dom->loadHTML($content);

      if ($dom == FALSE) {
        echo "FAAAAIL\n";
      }

      $xpath = new DOMXPath($dom);

      $tags = $xpath->query("/html");

      foreach ($tags as $tag) {
        var_dump(trim($tag->nodeValue));
      }
?>

编辑:

因此,根据 WBAR 的建议(谢谢),我正在寻找一种方法来更改 file_get_contents() 函数中的标头,这是我在其他地方找到的答案。现在我能够获得该网站的 HTML,希望我能处理这个烂摊子的解析:D

<?php
    libxml_use_internal_errors(true);
    // Create a stream
    $opts = array(
      'http'=>array(
        'user_agent' => 'PHP libxml agent', //Wget 1.13.4
        'method'=>"GET",
        'header'=>"Accept-language: en\r\n" .
                  "Cookie: foo=bar\r\n"
      )
    );
    $context = stream_context_create($opts);

    // Open the file using the HTTP headers set above
    $content = file_get_contents('http://www.cinemacity.cz/', false, $context);

    $dom = new DomDocument;
    $dom->loadHTML($content);

    if ($dom == FALSE) {
        echo "FAAAAIL\n";
    }

    $xpath = new DOMXPath($dom);

    $tags = $xpath->query("/html");

    foreach ($tags as $tag) {
        var_dump(trim($tag->nodeValue));
    }
?>

【问题讨论】:

  • 天啊,他们的代码真的是一团糟。 html之间有很多换行符。也许dom解析器对此感到困惑?我不知道。在将它提供给 dom 解析器之前尝试在多个换行符上使用 replace_all?
  • 如果你从命令行使用这个默认的 php 配置不允许外部 url 获取,检查一下!

标签: php html-parsing file-get-contents


【解决方案1】:

问题不在于 PHP,而在于目标主机。它检测客户端的 User-Agent 标头。看看这个:

wget http://www.cinemacity.cz/
2012-10-07 13:54:39 (1,44 MB/s) - saved `index.html.1' [234908]

但是当删除 User-Agent 标头时:

wget --user-agent="" http://www.cinemacity.cz/
2012-10-07 13:55:41 (262 KB/s) - saved `index.html.2' [4/4]

服务器只返回了 4 个字节

【讨论】:

    【解决方案2】:

    尝试以这种方式获取内容:

      function get2url($url, $timeout = 30, $port = 80, $buffer = 128) {
        $arr = parse_url($url);
        if(count($arr) < 3) return "URL ERROR";
    
        $ssl = "";
        if($arr['scheme'] == "https") $ssl = "ssl://";
    
        $header  = "GET " . $arr['path'] . "?" . $arr['query'] . " HTTP/1.0\r\n";
        $header .= "Host: " . $arr['host'] . "\r\n";
        $header .= "\r\n";
    
        $f = @fsockopen($ssl . $arr['host'], $port, $errno, $errstr, $timeout);
    
        if(!$f)
          return $errstr . " (" . $errno . ")";
    
        else{
          @fputs($f, $header . $arr['query']);
    
          $echo = "";
          while(!feof($f)) { $echo .= @fgets($f, $buffer); }
    
          @fclose($f);
    
          return $echo;
        }
      }
    

    不过,您必须删除标题。

    【讨论】:

      猜你喜欢
      • 2015-01-24
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 2021-09-15
      相关资源
      最近更新 更多