<link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7538664413577532945/posts/default/6420470017556836335'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7538664413577532945/posts/default/6420470017556836335'/><link rel='alternate' type='text/html' href='http://tipsorangsukses.blogspot.com/2013/05/tips-membeli-emas-batangan.html' title='Tips Membeli Emas Batangan'/>

i want to get link rel alternate href only, so the result when echo is http://tipsorangsukses.blogspot.com/2013/05/tips-membeli-emas-batangan.html

i wrote like this but still wrong

$feed = file_get_contents("http://tipsorangsukses.blogspot.com/atom.xml");
$xml = new SimpleXmlElement($feed);
echo "<ul>";

foreach($xml->entry as $entry) {
$linku = mysql_real_escape_string("$entry->link");
echo $linku;
}
echo "</ul>";

please help me, daniweb friends :)

Recommended Answers

All 2 Replies

Hello,

first, are you sure your script can access the link? Because from the browser, it displays fine, but from script it returns error 503, that's due to a restriction. I had to set a user agent through a stream context to the file_get_contents():

// Create a stream
$opts = array(
        'http' => array(
            'method' => "GET",
            'header' => "Accept-language: en\r\n" .
            "User-Agent: Mozilla/5.0 (X11; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0\r\n"
        )
    );

$context = stream_context_create($opts);

$feed = file_get_contents("http://tipsorangsukses.blogspot.com/atom.xml", false, $context);

Now, the loop:

$xml = new SimpleXmlElement($feed);

foreach($xml->entry as $entry)
{
    print_r($entry);
    break;
}

If you use print_r($entry), and break;, you can see how the first entry is returned:

SimpleXMLElement Object
(
    [id] => tag:blogger.com,1999:blog-7538664413577532945.post-6420470017556836335
    [published] => 2013-05-29T07:59:00.001+08:00
    [updated] => 2013-05-29T07:59:19.588+08:00
    [category] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [scheme] => http://www.blogger.com/atom/ns#
                    [term] => Bisnis
                )

        )

    [title] => Tips Membeli Emas Batangan
    [summary] => 


Emas batangan merupakan salah satu investasi cerdas dan prestisius mengingat kadar kemurniannya yang mencapai 24 karat dan sejak dahulu telah menjadi investasi bagi kalangan masyarakat menengah ke atas. Meskipun begitu, di masa ini telah banyak peluang untuk memiliki emas batangan sebagai investasi bagi masyarakat kalangan menengah ke bawah dari berat minimum 5 gram emas hingga kelipatannya 
    [link] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => edit
                            [type] => application/atom+xml
                            [href] => http://www.blogger.com/feeds/7538664413577532945/posts/default/6420470017556836335
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => self
                            [type] => application/atom+xml
                            [href] => http://www.blogger.com/feeds/7538664413577532945/posts/default/6420470017556836335
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [rel] => alternate
                            [type] => text/html
                            [href] => http://tipsorangsukses.blogspot.com/2013/05/tips-membeli-emas-batangan.html
                            [title] => Tips Membeli Emas Batangan
                        )

                )

        )

    [author] => SimpleXMLElement Object
        (
            [name] => livi marley
            [uri] => https://plus.google.com/108465856332941476207
            [email] => noreply@blogger.com
        )

)

As you see, the link index is an array, so to get access the first link of this entry, you can write:

echo $entry->link[0]->attributes()->href;

To go through the array and match the alternate versions, you need an extra loop inside the first one and then you can check the value of the rel attribute:

$xml = new SimpleXmlElement($feed);
foreach($xml->entry as $entry)
{
    foreach($entry->link as $link)
    {
        if($link->attributes()->rel == 'alternate')
        {
            echo $link->attributes()->href;
        }
    }
}

This should print all the alternate links. If it doesn't work explain the error you get. Bye!

thanks cereal, it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.