简体   繁体   中英

PHP parse xml and return first of child type in node

I am using cURL to get some XML from a blog. I want to loop through the XML and output list items that consist of the link wrapped around the title IE <li><a href="link">Title</a></li> Easy enough. The issue is that there are 3 <links> in each <entry> node.

The first one is correct, the other two have incorrect directory structures and hash's that don't open the post when clicked. Currently I am just trimming the known added directories using str_replace . If the added directories "/feed/atom" change, that won't work in my case, so it's not a good solution. I want to do something like $link[0] to only return the first link.

Simplified returned 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 only need the first one, in this case <link href="http://www.url1.com" />

str_replace in action now

<?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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM