4

I have an XML feed that i could fetch news about cyprus on a news site. I want to use news images and of course news itself.

Here is a sample of xml

 <item>
      <title>Rumlardan KKTC üniversitelerine ambargo</title>
      <description>
        <![CDATA[<p><a href="http://www.ntvmsnbc.com/id/25368624/">
          <img align="left" border="0" src="http://media.ntvmsnbc.com/j/NTVMSNBC/Components/ArtAndPhoto-Fronts/Sections-StoryLevel/Dünya/Kıbrıs/120723girne.thumb.jpg" alt="" style="margin:0 5px 5px 0" /></a> 
          Kıbrıs Rum yönetimi, KKTC'deki üniversitelerle işbirliği yapan ülkelerin eğitim kurumlarına resmi yazılar göndererek yetkilileri tehdit ediyor. Hindistan üniversitesinden ortak akademik programlara son verilmesi istendi.</p><br clear="all" />]]>
      </description>
      <link>http://www.ntvmsnbc.com/id/25368624/</link>
      <media:content medium="image" url="http://media.ntvmsnbc.com/j/NTVMSNBC/Components/ArtAndPhoto-Fronts/Sections-StoryLevel/Dünya/Kıbrıs/120723girne.thumb.jpg">
        <media:text>
          <![CDATA[<p><a href="http://www.ntvmsnbc.com/id/25368624/">
            <img align="left" border="0" src="http://media.ntvmsnbc.com/j/NTVMSNBC/Components/ArtAndPhoto-Fronts/Sections-StoryLevel/Dünya/Kıbrıs/120723girne.thumb.jpg" alt="" style="margin:0 5px 5px 0" />
            </a>
            </p>
            <br clear="all" />]]>
        </media:text>
      </media:content>
      <pubDate>Mon, 23 Jul 2012 14:27:32 GMT</pubDate>
      <category>Haberler</category>
      <guid isPermaLink="true">http://www.ntvmsnbc.com/id/25368624/</guid>
    </item>

The link of the xml is : http://www.ntvmsnbc.com/id/24928068/device/rss/rss.xml

Now i parse this xml with simpleXML with using below code. On this code i could prevent to display <img> tag with strip_ tags i could only display text data in CDATA.

In short what i am asking is how can i get media:content url="" into my code as I want to change thumb.jpg to hlarge.jpg in that url .

I tried $d['media'] = $news->media->attributes(); '<p>'.$post['media']['url']. but it's not working

Here is my code :

<? 
$NewsFeedUrl =  "http://www.ntvmsnbc.com/id/24928068/device/rss/rss.xml";

$xml = @simplexml_load_file($NewsFeedUrl);

if(is_object($xml)){
    //Rest of our code will be here
}else{
    die('Güncel Haberlere Bağlanılamıyor.');
}

foreach($xml->channel->item as $news){
    if(is_array($newsContent) && count($newsContent)==$amountToShow){
    }
    $description = $news->description;
    $d['title'] =$news->title;
    $d['link'] = $news->link;
    $d['media'] = $news->media->attributes();
    $d['cont'] = $news->description;
    $d['date'] = $news->pubDate;
    $newsContent[]=$d;
}
//$ad=array("thumb", "left");

if(is_array($newsContent)){
    foreach($newsContent as $post){

        echo '

      <article class="entry"><h3>'.'<a href="'.$post['link'].' "target="_blank">'.$post['title'].'</a></h3>
      '.'<div class="meta"><span class="date_post">'.$post['date'].'</span>'.$post['pubDate'].
      //'<p>'.str_replace($ad,"hlarge",$post['cont']).
      '<p>'.$post['cont'].
      '<p>'.strip_tags($post['cont']).
      //'<p>'.$post['media']['url'].
      '<p><a href="'.$post['link'].' "target="_blank" class="button">Devamını Oku</a>'.
      ' </article>';
    }
}else{
    echo '<p>Güncel Haberler Alınamadı Sayfayı Yenilemeyi Deneyin.</p>';
}

?>
Makesh
  • 1,236
  • 1
  • 11
  • 25
orko
  • 117
  • 2
  • 11

2 Answers2

3

The content element has a namespace prefix (<media:content>), so it can’t be accessed by the usual means.

Namespace-URI for "media" is from http://search.yahoo.com/mrss/ (check your rss.xml for "xmlns:media").

Try this:

foreach ($xml->channel->item as $news)
{
    $ns_media = $news->children('http://search.yahoo.com/mrss/');

    echo $ns_media->content; // displays "<media:content>"
}

EDIT :

I think there is some prob with the namespace uri "http://search.yahoo.com/mrss/"

I tried using your xml :

http://codepad.org/P90bOQUj [not working]

I tried using other xml :

http://codepad.org/ADYveL6T [working]

hakre
  • 193,403
  • 52
  • 435
  • 836
Makesh
  • 1,236
  • 1
  • 11
  • 25
  • i add your suggestion as `foreach($xml->channel->item as $news){ if(is_array($newsContent) && count($newsContent)==$amountToShow){ } $description = $news->description; $d['title'] =$news->title; $d['link'] = $news->link; $d['media'] = $news->media->attributes(); $d['cont'] = $news->description; $d['date'] = $news->pubDate; $ns_media = $news->children('http://search.yahoo.com/mrss/'); $ns_media->content; $newsContent[]=$d; }` `echo '

    '.$ns_media. }` however did not work

    – orko Jul 25 '12 at 12:52
  • I tried with other xml file (other namespace ) . for me it worked .. working with your xml file – Makesh Jul 25 '12 at 13:28
1

I was having this problem too, I tested using this solution:

https://gist.github.com/enderandpeter/6b760140bf9d2ed9620c

And it worked!

$content = $xml->channel->item->children('media', true)->content;

$contentattr = $content->attributes();
Pablo Camara
  • 622
  • 1
  • 5
  • 14