Php RSS源url返回为空
我正在尝试从Wordpress.php页面中的外部xml文件呈现rss提要:Php RSS源url返回为空,php,xml,wordpress,rss,feed,Php,Xml,Wordpress,Rss,Feed,我正在尝试从Wordpress.php页面中的外部xml文件呈现rss提要: <?php $rss = new DOMDocument(); $rss->load('http://feeds.bbci.co.uk/news/rss.xml?edition=uk'); $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $ite
<?php
$rss = new DOMDocument();
$rss->load('http://feeds.bbci.co.uk/news/rss.xml?edition=uk');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
我不能随意引用这个有问题的url,但我希望只有少数情况下,在浏览器中呈现良好效果的有效url不能使用此方法分配给变量
bbc源代码的开头行:(工作):
打开所需源代码的行(不工作):
各自的XML结构几乎相同,并共享一个共同点:
<channel><title>
<channel><link>
<channel><description>
……等等。我的问题是,为什么这个冒犯性的url不起作用?先谢谢你
注意:我对php/xml的知识还处于相当初级的水平,所以外行的术语和乐高积木的简单解释很受欢迎
更新
终于成功了——这位同事试图查看提要url是否被压缩——结果被压缩了。因此,更新的代码是:
function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$foo = download_page('http://xxxxxxxx.xml');
$rss = new DOMDocument();
$rss->loadXML($foo);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
函数下载页面($path){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$path);
curl_setopt($ch,CURLOPT_FAILONERROR,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_超时,15);
curl_setopt($ch,CURLOPT_编码,“gzip”);
$retValue=curl\u exec($ch);
卷曲关闭($ch);
返回$retValue;
}
$foo=下载页面('http://xxxxxxxx.xml');
$rss=新的DOMDocument();
$rss->loadXML($foo);
$feed=array();
foreach($rss->getElementsByTagName('item')作为$node){
$item=数组(
'guid'=>$node->getElementsByTagName('guid')->项(0)->nodeValue,
'title'=>$node->getElementsByTagName('title')->项(0)->nodeValue,
'link'=>$node->getElementsByTagName('link')->项(0)->nodeValue,
'date'=>$node->getElementsByTagName('pubDate')->项(0)->节点值,
'desc'=>$node->getElementsByTagName('description')->项(0)->节点值,
);
阵列推送($feed,$item);
}
$limit=3;
对于($x=0;$x)你能不能把“它不起作用”的内容再扩展一点?你有没有办法把你不能透露的文件拿出来,制作一个经过审查的版本,这样(a)你就可以给我们看,(b)仍然会引发你所看到的任何问题——即使里面只有一篇假文章?(另外,有问题的提要是否在进行验证?这可能会很快告诉您提要是否存在某种错误,或者提供一些线索。)嗨,Matt,感谢您的及时回复,feed在feedvalidator上得到了成功验证,并提供了一些建议…我将尝试在不久的将来获得一个适当的远程文件,并将其托管在某个地方…我想我离此有点近了:我查看了有问题的rss xml文件的源,并将其保存为一个新的.xml文件,然后将其放在另一个主机上ing服务器-工作正常!这是否表明我的问题是原始.xml文件的托管位置存在某种配置/权限问题?或者我应该在“加载”中使用某些参数?谢谢您的帮助!我不确定。可能是某种编码问题(复制/粘贴/保存文件可能会在您不一定注意的情况下修复),或者权限问题,甚至源服务器正在以某种特定方式检查请求头。如果没有实际的源URL,则很难对此进行调试。如果只执行以下操作,您会看到什么:$feed\u source=file\u get\u contents('http://that.feed.url/...“);echo$feed_source;
?您应该会看到feed内容,但我想知道它是否真的会向您显示一个错误,这可能有助于了解发生了什么。
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:tp="http://www.xxxxxxxx.co.uk/rss/">
<channel><title>
<channel><link>
<channel><description>
function download_page($path){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$path);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
$retValue = curl_exec($ch);
curl_close($ch);
return $retValue;
}
$foo = download_page('http://xxxxxxxx.xml');
$rss = new DOMDocument();
$rss->loadXML($foo);
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue,
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}