Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果字段为空,如何跳过RSS项目?_Php_Xpath_Rss_Domdocument - Fatal编程技术网

Php 如果字段为空,如何跳过RSS项目?

Php 如果字段为空,如何跳过RSS项目?,php,xpath,rss,domdocument,Php,Xpath,Rss,Domdocument,另一件小事让我抓狂。。。我想拉入一个RSS提要并使用PHP显示它。它要求mrss说明(因为它没有附加所有额外的垃圾)。唯一的问题是feed有广告。因此,当脚本调用广告的媒体描述(没有广告描述)时,整个页面将失败 我试着根据长度使用continue和if…else,但仍然不起作用 $xml=("http://feeds.abcnews.com/abcnews/gmavideos"); $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); $x=$

另一件小事让我抓狂。。。我想拉入一个RSS提要并使用PHP显示它。它要求mrss说明(因为它没有附加所有额外的垃圾)。唯一的问题是feed有广告。因此,当脚本调用广告的媒体描述(没有广告描述)时,整个页面将失败

我试着根据长度使用continue和if…else,但仍然不起作用

$xml=("http://feeds.abcnews.com/abcnews/gmavideos");
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');

for ($i=0; $i<=10; $i++)
  {
  $item_title=$x->item($i)->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_link=$x->item($i)->getElementsByTagNameNS('http://www.pheedo.com/namespace/pheedo', 'origLink')
  ->item(0)->childNodes->item(0)->nodeValue;
  $item_desc=$x->item($i)->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'description')
  ->item(0)->childNodes->item(0)->nodeValue;

    if ($x->length == 0) {
        exit('etc.');
    }
    else {
  echo ("<p><a href='" . $item_link
  . "' target='_blank'>" . $item_title . "</a>");
  echo ("<br />");
  echo ($item_desc . "</p>");
  }
  }
$xml=(“http://feeds.abcnews.com/abcnews/gmavideos");
$xmlDoc=新的DOMDocument();
$xmlDoc->load($xml);
$x=$xmlDoc->getElementsByTagName('item');
对于($i=0;$iitem($i)->getElementsByTagName('title'))
->项(0)->子节点->项(0)->节点值;
$item_link=$x->item($i)->getelementsbytagnames($i)http://www.pheedo.com/namespace/pheedo“,”origLink“)
->项(0)->子节点->项(0)->节点值;
$item_desc=$x->item($i)->getelementsbytagnames($s)http://search.yahoo.com/mrss/“,”说明“)
->项(0)->子节点->项(0)->节点值;
如果($x->length==0){
出口(等);
}
否则{
回声();
回声(
); 回声($item_desc.“

”); } }

感谢您的指导!

在没有看到错误消息的情况下,我无法确定,但脚本很可能因为以下行而失败:

$item_desc=$x->item($i)->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'description')->item(0)->childNodes->item(0)->nodeValue;
如果没有description元素,那么
getelementsbytagnames()
将返回一个空的
DOMNodeList
,因此
->item(0)->childNodes
将失败

你需要把这些分开

$item_desc_nodes = $x->item($i)->getElementsByTagNameNS('http://search.yahoo.com/mrss/', 'description');
if ($item_desc_nodes->length) {
    $item_desc = $item_desc_nodes->item(0)->childNodes->item(0)->nodeValue;
} else {
    continue;
}
不过,老实说,您确实应该使用XPath而不是
getElementsByTagnames

您还应该使用
->textContent
而不是
->childNodes->item(0)->nodeValue

更新:这就是我如何使用XPath完成相同任务的方法

$xml = 'http://feeds.abcnews.com/abcnews/gmavideos';
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

$xpath = new DOMXPath($xmlDoc);
$xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
$xpath->registerNamespace('pheedo', 'http://www.pheedo.com/namespace/pheedo');

// filter out the ads in the xpath expression itself by saying there must be
// a pheedo:origLink element in the item
$items = $xpath->query('/rss/channel/item[pheedo:origLink][position() < 11]');

$linktmpl = '<p><a href="%s" target="_blank">%s</a><br />%s</p>'."\n";

foreach($items as $item) {
    $item_title = $xpath->evaluate('string(title)', $item);
    $item_link = $xpath->evaluate('string(pheedo:origLink)', $item);
    $item_desc = $xpath->evaluate('string(media:description)', $item);

    $escaped = array_map('htmlspecialchars', array($item_link, $item_title, $item_desc));
    vprintf($linktmpl, $escaped);
}
$xml='1!'http://feeds.abcnews.com/abcnews/gmavideos';
$xmlDoc=新的DOMDocument();
$xmlDoc->load($xml);
$xpath=newdomxpath($xmlDoc);
$xpath->registerNamespace('media','http://search.yahoo.com/mrss/');
$xpath->registerNamespace('pheedo','http://www.pheedo.com/namespace/pheedo');
//过滤掉xpath表达式本身中的广告,方法是说必须有
//项目中的一个pheedo:origLink元素
$items=$xpath->query('/rss/channel/item[pheedo:origLink][position()<11]');
$linktmpl='
%s

'。“\n”; foreach($items作为$item){ $item_title=$xpath->evaluate('string(title)'$item); $item_link=$xpath->evaluate('string(pheedo:origLink)'),$item); $item_desc=$xpath->evaluate('string(media:description)'),$item; $escape=array_map('htmlspecialchars',array($item_link,$item_title,$item_desc)); vprintf($linktmpl,$Escape); }
唯一的问题是feed有广告。所以当 脚本调用广告的媒体描述(用于 没有),整个页面失败

使用

/*/channel/item[media:description]
/*/channel/item[not(title='Advertisement:')]
或者,或者

/*/channel/item[media:description]
/*/channel/item[not(title='Advertisement:')]

我个人会使用并推荐上面的第一个表达式。

这没有帮助。PHP会在分配
$item\u title
之前完全出错,因为OP的链接没有检查
DOMNodeList
是否有任何项。嗯..不会。你需要在你的范围内使用它。如果你运行OP的代码,你可以当循环到达“广告”项时获取此错误:
PHP致命错误:调用rss\u item\u test.PHP第12行的非对象上的成员函数item()
。因此,永远不会有空白的
$item\u title
要测试——它没有被分配,因为在遇到致命错误之前,右侧尚未完成计算。