我试图用php解析rss提要,但它什么也没给我。正如你所看到的,我正在尝试回显$doc和$itemRSS数组,但当数组中的项0从未到达时,它什么也没给我,只给我一个"成功!"。
如果有人能说"你想过(愚蠢的错误)吗?",我会很激动,所以请考虑我这个问题。非常感谢。
$doc = new DOMDocument();
$doc->load('http://pipes.yahoo.com/pipes/pipe.run?_id=566903fd393811762dc74aadc701badd&_render=rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'guid' => $node->getElementsByTagName('guid')->item(0)->nodeValue
);
array_push($arrFeeds, $itemRSS);
}
if ($itemRSS[0] != NULL) {
echo 'Success!';
}
echo $itemRSS;
echo $doc;
我的意思是,这一页完全是空白的。没有错误,什么都没有。
更新:很明显,我的网络主机已经禁用了allow_url_fopen,所以我必须找到另一种方法来做到这一点叹息
正如其他答案中所提到的,您做错了几件事,比如试图回显数组和对象。出于某种原因,你在$arrFeeds中也没有得到任何结果,尽管你应该这样做。
一种更简单的方法是将提要的呈现方法更改为JSON:http://pipes.yahoo.com/pipes/pipe.run?_id=566903fd393811762dc74aadc701badd&_render=json
然后您可以使用json_decode()来获得所有项目的数组:
$contents = json_decode(file_get_contents('http://pipes.yahoo.com/pipes/pipe.run?_id=566903fd393811762dc74aadc701badd&_render=json'));
foreach($contents['items'] as $item) {
// use $item['title'], $item['description'] etc...
}
请注意,您只能回显字符串、int等,而不能回显数组或对象等结构化数据。
为了更容易,您可以在浏览器中打开该URL并分析JSON内容@http://json.parser.online.fr/-然后您将看到数组的结构。
JSON是一种更容易使用IMO.的格式
编辑:
由于file_get_contents()被禁用,您可以使用cURL(它应该安装在大多数服务器上,尤其是在禁用allow_url_fopen的情况下):
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
在脚本末尾添加这些行并进行检查。
var_dump($itemRSS);
var_dump($arrFeeds);
var_dump($doc);
问题是知道你没有正确显示信息。
除了尝试回显一个对象和一个数组外,脚本没有任何问题,print_r$arrFeeds您可以看到的所有内容