Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.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解析xml并返回节点中第一个子类型_Php_Xml - Fatal编程技术网

PHP解析xml并返回节点中第一个子类型

PHP解析xml并返回节点中第一个子类型,php,xml,Php,Xml,我正在使用cURL从博客中获取一些XML。我想循环浏览XML和输出列表项,这些项目由围绕标题的链接组成,非常简单。问题是每个节点中有3个 第一个是正确的,另外两个有不正确的目录结构和散列,当点击时不会打开帖子。目前,我正在使用str\u replace修剪已知添加的目录。如果添加的目录“/feed/atom”发生更改,那么在我的情况下,这将不起作用,因此这不是一个好的解决方案。我想做一些类似$link[0]的操作,只返回第一个链接 简化的返回xml <entry> <a

我正在使用cURL从博客中获取一些XML。我想循环浏览XML和输出列表项,这些项目由围绕标题的链接组成,非常简单。问题是每个
节点中有3个

第一个是正确的,另外两个有不正确的目录结构和散列,当点击时不会打开帖子。目前,我正在使用
str\u replace
修剪已知添加的目录。如果添加的目录“/feed/atom”发生更改,那么在我的情况下,这将不起作用,因此这不是一个好的解决方案。我想做一些类似$link[0]的操作,只返回第一个链接

简化的返回xml

<entry>
    <author>
        <name>Name</name>
        <uri>http://www.url.com</uri>
    </author>
    <title>Title</title>
    <link href="http://www.url1.com" />
    <link href="http://www.url2.com#comments" />
    <link href="http://www.url3.com/feed/atom/" />
</entry>

";;
回声“”;
如果(++$i==3)中断;
}
?>

更新了输出标记的答案。我不理解您不理解的内容。只需将下载的xml传递给该函数即可从中获取链接?不返回任何内容。是否有某种方法可以只执行$link=$item->link[0]就行了?我使用$xml='“/>”对其进行了测试;而不是下载_page()函数,一切正常。你做错了,当下载页面返回的内容与你发布的内容不一致时,这个答案是有效的。只要用xml字符串替换下载页面,您就会看到。
<?php
function download_page($path){
    //cURL stuff, all good
}

$sXML = download_page('http://example.org/feed/atom/');
$oXML = new SimpleXMLElement($sXML);

$items = $oXML->entry;
$i = 0;
foreach($items as $item) {
    $title = $item->title;
    $link = $item->link;
    //$link = $item->link[0] or {0} neither works. Want the first one in the <entry> node
    echo '<li>';
    foreach($link as $links) {
        $loc = $links['href'];
            $href = str_replace("/feed/atom/", "", $loc);
            echo "<a href=\"$href\" target=\"_blank\">";
    }
    echo $title;
    echo "</a>";;
    echo "</li>";
    if(++$i == 3) break;
}
?>
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);
    $retValue = curl_exec($ch);          
    curl_close($ch);
    return $retValue;
}
$xml = download_page('http://foo.com/tradeblog/feed/atom/');

function getHref($__xml){
    $xml = new SimpleXMLElement($__xml);
    foreach($xml as $node){     
        foreach($node->attributes() as $prop => $val){
            if($prop === 'href'){
                $p = strrpos($val, '/');
                $val = substr($val, $p);
                return $val;
            }           
        }           
    }
}

$link = getHref($xml);
echo $link;