Parser RSS e DOMDocument::load


Parser RSS e DOMDocument::load

我有一个读取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设置可能会导致问题?

由于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);
?>

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