Page 1 of 1

trying to extract image url from an xml document (rss feed)

Posted: Mon Jun 02, 2008 7:20 am
by bottleweb
Hello, I'm currently building an RSS feed agregator in php. I am able to obtain, store and display the title, descirption, etc. but haven't been able to obtain images from the xml files. Here is the table structure of a common rss xml file (this one is from yahoo):

Code: Select all

<item>
    <title>story title</title> 
    <link>http://story_url</link> 
    <guid isPermaLink="false">/link/link2</guid> 
    <source>AP</source> 
    <pubDate>Mon, 02 Jun 2008 11:20:14 GMT</pubDate> 
    <description>description goes here</description> 
    <media:content url="http://imageurl.jpg" type="image/jpeg" height="130" width="111" /> 
    <media:text type="html"><p><a href="linkurl"><img src="http://imageurl.jpg" align="left" height="130" width="111" alt="photo" title="description" border="0"/></a></p><br clear="all"/></media:text> 
    <media:credit role="publishing company">(AP)</media:credit> 
</item>
What i'm interested in is the media:content 'url' variable, which stores the image URL. So far I've got the following code (works fine for title, description and URL):

Code: Select all

foreach ($xml->item as $item) {
        
        $rss_feed_title = trim(strval($item->title));
        $rss_feed_url = trim(strval($item->link));
        $rss_feed_description = trim(strval($item->description));
        
    //try to obtain the image as well (this doesn't work)
    $rss_feed_image = trim(strval($item->media['url']));
             
             //insert code here to store the values in a database
}
The problem is this line:

Code: Select all

$rss_feed_image = trim(strval($item->media['url']));
which doesn't work. I'm hoping someone can tell me what I'm doing wrong here.

Re: trying to extract image url from an xml document (rss feed)

Posted: Mon Jun 02, 2008 5:21 pm
by bottleweb
I still haven't found a solution to this problem, surely someone can help me with this? I should clarify that I'm using:

$xml = simplexml_load_string($received_rss_feeds) to get the xml array in the first place.