Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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/0/xml/13.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中xpath创建的数组中的数组项_Php_Xml_Xpath_Simplexml_Xml Namespaces - Fatal编程技术网

正确显示php中xpath创建的数组中的数组项

正确显示php中xpath创建的数组中的数组项,php,xml,xpath,simplexml,xml-namespaces,Php,Xml,Xpath,Simplexml,Xml Namespaces,我正在尝试制作一个rss阅读器/抓取器/解析器,或者任何你想称之为rss阅读器/抓取器/解析器的东西。 提要url为http://www.jutarnji.hr/rss 我制作了一个php函数,除了从rss项中获取图像外,其他一切都可以运行,因为这是一个非常复杂的rss提要,唯一简单的方法是使用xpath抛出一个数组,我不知道如何在foreach循环中自动提取该数组,以显示每个提要项旁边的图像 注意:这不是我的rss源,我正在尝试获取其他人的rss源: function parser($feed

我正在尝试制作一个rss阅读器/抓取器/解析器,或者任何你想称之为rss阅读器/抓取器/解析器的东西。 提要url为http://www.jutarnji.hr/rss

我制作了一个php函数,除了从rss项中获取图像外,其他一切都可以运行,因为这是一个非常复杂的rss提要,唯一简单的方法是使用xpath抛出一个数组,我不知道如何在foreach循环中自动提取该数组,以显示每个提要项旁边的图像 注意:这不是我的rss源,我正在尝试获取其他人的rss源:

function parser($feedURL) {
$rss = simplexml_load_file($feedURL);
$rss->registerXPathNamespace('n', 'http://jutarnji.hr/rss');

$i = 0;
foreach ($rss->entry as $feedItem) {
    $i++;
    $myDate = ($feedItem->updated);
    $dateForm = explode(" ", $myDate);
    if (date("j.n.Y.", strtotime($myDate)) == date("j.n.Y.")) {
    $niceDate = date("H:i", strtotime($myDate));
    }
    else if (date("j.n.Y.", strtotime($myDate)) == date("j.n.Y.", strtotime("yesterday"))) {
    $niceDate = "jučer u " . date("H:i", strtotime($myDate));
    }
    else {
    $niceDate = date("j.n.Y.", strtotime($myDate)) . " u " . date("H:i", strtotime($myDate));  
    }
    $feedurl = ($feedItem->link->attributes()->href);

    $imgUrl = $rss->xpath("//n:link[@rel='enclosure']/@href");  //This throws an array which I cannot extract and use in this foreach loop as well

    $urltoimage = (string)$imgUrl[0][0][1]; // This was my try to atleast get a specific item but not working

    echo "<div class='box'>
        <h3><a target='_blank' href='$feedurl' title='$feedItem->title'>" . $feedItem->title . "</a></h3>
        <p><img src='$urltomage'/>" . $feedItem->description . "</p>
        <p class='belowpost'><img src='images/time.png'/>" . $niceDate. "<a class='cont' target='_blank' href='$feedurl' title='$feedItem->title'>Više</a></p></div>";
    if($i >= 20) break;
} }
任何帮助都是感激的,只要没有书包括lol
提前感谢,这对我来说非常重要

这一行有一些错误

$imgUrl = $rss->xpath("//n:link[@rel='enclosure']/@href");
假设您喜欢从rss项目获取图像。 您注册了名称空间http://jutarnji.hr/rss 但是链接节点的名称空间是http://www.w3.org/2005/Atom.

您正在使用文档节点$rss作为xpath的上下文节点,但需要使用当前条目节点$feedItem。Xpaht以//开头,它将查找整个文档中的所有链接节点,即使您使用了正确的上下文节点

因此: 添加正确的命名空间:

 $feedItem->registerXPathNamespace('n', 'http://www.w3.org/2005/Atom');
而不是使用:

$imgUrl = $feedItem->xpath("n:link[@rel='enclosure']/@href"); 
或者,如果链接不是直接子链接:

$imgUrl = $feedItem->xpath(".//n:link[@rel='enclosure']/@href"); 
而不是从第一个imgUrl获取链接:

$urltoimage = (string)$imgUrl[0]->href;

尝试对$imgUrl执行一个var_转储,就像这个var_转储$imgUrl;。把你的问题贴上。您可能会发现它不是一个数组,而是一个stdObject.paste var_dump$imgUrl;我认为您的命名空间注册应该是$rss->registerXPathNamespace'n','http://www.w3.org/2005/Atom'; 查看提要开始。但是有必要吗?当我以XPath2.0和XML源作为输入运行//link[@rel='enclosure']/@href时,我会得到您针对默认名称空间?的所有URL?。您也可以尝试//n:link[@rel='enclosure']/@n:href
$urltoimage = (string)$imgUrl[0]->href;