我使用DOMDocument()在我的代码中包含RSS提要。然而,我得到了这个错误:
在服务器配置中禁用URL文件访问。
这是因为我的服务器既不允许修改php.ini文件,也不允许将allow_url_fopen设置为ON。
有办法解决这个问题吗?这是我的完整代码:
<?php
$rss = new DOMDocument();
$rss->load('rss.php');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
echo '<table>';
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
echo <<<EOF
<tr>
<td><a href="$link"><b>$title</b></a></td>
</tr>
EOF;
}
echo '</table>';
?>谢谢。
发布于 2014-02-07 06:33:13
好吧,我自己解决了。
<?php
$k = 'rss.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $k);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$rss = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($rss, 'SimpleXMLElement', LIBXML_NOCDATA);
$feed = array();
foreach($xml->channel->item as $item){
$item = array (
'title' => $item->title,
'desc' => $item->description,
'link' => $item->link,
'date' => $item->pubDate,
);
array_push($feed, $item);
}
$limit = 5;
echo '<table>';
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
echo <<<EOF
<tr>
<td><a href="$link"><b>$title</b></a></td>
</tr>
EOF;
}
echo '</table>';
?>发布于 2014-02-04 07:11:20
使用cURL命令。您确实应该将它用于服务器到服务器之间的交互,而不是试图以任何方式将URL传递给构造函数。
这是cURL文档- http://us1.php.net/curl
我还有一个简单的基于cURL的REST客户机,您可以随意使用- https://github.com/mikecbrant/php-rest-client。
基本上,您所要做的就是使用cURL检索远程内容,而不是尝试使用fopen包装器直接打开它。检索完内容之后,将其传递给DOMDocument。
https://stackoverflow.com/questions/21546014
复制相似问题