解析器RSS e DOMDocument :: load

时间:2011-06-24 12:50:38

标签: php xml domdocument

我在脚本中遇到一个小问题,该脚本读取XML文件并打印输出:

<?php

  $doc = new DOMDocument();
  $doc->load("http://www.tripadvisor.it/Feeds-d235955-treviews.xml");
  foreach ($doc->getElementsByTagName('item') as $node) {
      echo $node->getElementsByTagName('title')->item(0)->nodeValue;
      echo $node->getElementsByTagName('description')->item(0)->nodeValue;
      echo $node->getElementsByTagName('link')->item(0)->nodeValue;
      echo $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
  }

?> 

如果您在我的个人域名(托管)上使用此脚本,它可以正常工作,但如果我在我的VPS上使用它不起作用并返回这些错误:

Warning: DOMDocument::load(http://www.tripadvisor.it/Feeds-d235955-treviews.xml) [domdocument.load]: failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /home/AAA/public_html/test.php on line 4
Warning: DOMDocument::load() [domdocument.load]: I/O warning : failed to load external entity "http://www.tripadvisor.it/Feeds-d235955-treviews.xml" in /home/AAA/public_html/test.php on line 4

哪些PHP或APACHE设置可能会导致问题?

2 个答案:

答案 0 :(得分:2)

由于file_get_contents在您的服务器中不起作用,因此请尝试使用curl与tripadvisor服务器连接,如下所示

<?php
$init = curl_init();
curl_setopt($init, CURLOPT_URL,'http://www.tripadvisor.it/Feeds-d235955-treviews.xml');
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($init);
curl_close ($init);
$xml = simplexml_load_string($contents);
print"<pre>";
print_r($xml);
?>

答案 1 :(得分:1)

尝试使用正则表达式在php中获取xml标记。 使用php curl获取xml然后使用正则表达式。 请尝试以下链接 http://www.bobulous.org.uk/coding/php-xml-regex.html