I have this code but I don't know how I get the media
Please someone could help me?
I need show the image of post on my website
Below my PHP and XML for Wordpress Feeds
PHP
$html = "";
$url = "https://intercambioemgalway.com.br/feed/";
$xml = simplexml_load_file( $url, 'SimpleXMLElement', LIBXML_NOCDATA );
for ($i = 0; $i < 5; $i++){
$img = $xml->channel->item->media;
$html .= "$img";
}
echo $html;
XML
<item>
<title>Qual escola vou estudar?</title>
<link>https://intercambioemgalway.com.br/2017/09/11/qual-escola-estudar/</link>
<comments>https://intercambioemgalway.com.br/2017/09/11/qual-escola-estudar/#respond</comments>
<pubDate>Mon, 11 Sep 2017 17:36:59 +0000</pubDate>
<dc:creator><![CDATA[admin]]></dc:creator>
<category><![CDATA[Meu intercâmbio]]></category>
<guid isPermaLink="false">https://intercambioemgalway.com.br/?p=179</guid>
<description><![CDATA[<p>Hoje vou contar um pouquinho da escola que vou estudar em Galway, a Atlantic Language.</p>]]></description>
<wfw:commentRss>https://intercambioemgalway.com.br/2017/09/11/qual-escola-estudar/feed/</wfw:commentRss>
<slash:comments>0</slash:comments>
<media:content url="https://intercambioemgalway.com.br/wp-content/uploads/2017/09/Untitled-1.jpg" type="image/jpeg" medium="image" width="900" height="600">
<media:title type="plain">
<![CDATA[atlantic-language-galway]]>
</media:title>
<media:thumbnail url="https://intercambioemgalway.com.br/wp-content/uploads/2017/09/Untitled-1-150x150.jpg" width="150" height="150" />
<media:description type="plain">
<![CDATA[]]>
</media:description>
<media:copyright>
admin
</media:copyright>
</media:content>
Each $item is of type SimpleXMLElement.
Now you can loop the children using the namespace http://search.yahoo.com/mrss/ and then you can access the attributes.
For example:
foreach($xml->channel->item as $item) {
foreach($item->children("http://search.yahoo.com/mrss/") as $media) {
$img = (string)$media->attributes()->url;
}
}
Related
I can't get the thumbnail images to display in the RSS feed items. Where do I put the code for the image to so up?
first tried the getElementsByTagName but of course that doesn't work, then I tried your solution- $item_img = $item->getElementsByTagNameNS('the namespace URI you found','thumbnail')
->item(0)->getAttribute('url');
But that brought back an error that broke the whole page. I think I understand that thumbnail is a sub element of media: and I see that it is part of jwplayer:feedid under media:group. So, does it go in the array like the other items? I tried it inside the array and outside as well. Where do I put it to display the thumbnail in my newsfeed item?
<?php
$rss = new DOMDocument();
$rss->load('https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = 5;
for($x=0;$x<$limit;$x++) {
$title = str_replace(' & ', ' & ', $feed[$x]['title']);
$link = $feed[$x]['link'];
$description = $feed[$x]['desc'];
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
Here is the xml file output I am trying to parse:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:jwplayer="http://rss.jwpcdn.com/">
<channel>
<title>Car Repairs</title>
<description>Car Repairs</description>
<jwplayer:kind>MANUAL</jwplayer:kind>
<jwplayer:feedid>IYxiCISJ</jwplayer:feedid>
<jwplayer:feed_instance_id>7beba58b-b2a2-4000-af11-1e43a7cb8680</jwplayer:feed_instance_id>
<jwplayer:link rel="first" href="https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss&page_offset=1&page_limit=500"/>
<jwplayer:link rel="last" href="https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss&page_offset=1&page_limit=500"/>
<item>
<title>Preparing the Audi A8 for Service- The American Garage</title>
<link>https://cdn.jwplayer.com/previews/ybcuKyZl</link>
<description>Putting Audi A8 in service mode.</description>
<pubDate>Mon, 17 Sep 2018 14:35:11 -0000</pubDate>
<guid isPermaLink="false">ybcuKyZl</guid>
<enclosure url="https://cdn.jwplayer.com/videos/ybcuKyZl-qQFQ3TOZ.mp4" type="video/mp4" length="515"/>
<jwplayer:feedid>IYxiCISJ</jwplayer:feedid>
<media:group>
<media:content url="https://cdn.jwplayer.com/manifests/ybcuKyZl.m3u8" medium="video" type="application/vnd.apple.mpegurl" duration="515"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-jTncGIBU.mp4" medium="video" type="video/mp4" duration="515" width="320" height="180" fileSize="19714361"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-i4o7KXqD.mp4" medium="video" type="video/mp4" duration="515" width="480" height="270" fileSize="29378682"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-XMc5nvLA.mp4" medium="video" type="video/mp4" duration="515" width="720" height="406" fileSize="40249178"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-qQFQ3TOZ.mp4" medium="video" type="video/mp4" duration="515" width="1280" height="720" fileSize="109187664"/>
<media:content url="https://cdn.jwplayer.com/videos/ybcuKyZl-MTvbpSOY.m4a" medium="video" type="audio/mp4" duration="515" fileSize="7304077"/>
<media:thumbnail url="https://cdn.jwplayer.com/thumbs/ybcuKyZl-720.jpg" width="720" />
<media:keywords>The American Garage,thermostat,timing belt audi,audi a8,audi 4.2,water pump,service mode,A8,audi,car repairs</media:keywords>
</media:group>
<jwplayer:track file="https://cdn.jwplayer.com/strips/ybcuKyZl-120.vtt" kind="thumbnails"/>
</item>
</channel>
</rss>
All that's needed is to pass the correct namespace URI to DOMDocument::getElementsByTagNameNS(). (Rather than hard-coding it you can get the attribute from the XML document if you think it will change.)
In addition, you can't escape data for HTML just by replacing & with & there are built-in functions for that purpose, use them for all your data.
<?php
$rss = new DOMDocument();
$rss->load('https://cdn.jwplayer.com/v2/playlists/IYxiCISJ?format=mrss');
// get the namespace URI
$mediaURL = $rss->documentElement->getAttribute("xmlns:media");
foreach ($rss->getElementsByTagName('item') as $node) {
$feed[] = [
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
// get the namespaced element value
'thumbnail' => $node->getElementsByTagNameNS($mediaURL, 'thumbnail')->item(0)->getAttribute('url'),
];
}
foreach ($feed as $item) {
$title = htmlspecialchars($item["title"]);
$link = htmlspecialchars($item["link"]);
$description = htmlspecialchars($item["desc"]);
$date = htmlspecialchars(date('l F d, Y', strtotime($item['date'])));
$thumbnail = htmlspecialchars($item["thumbnail"]);
echo <<< HTML
<p>
<a href="$link" title="$title">
<img src="$thumbnail" alt="$title"/>
<br/>
<strong>$title</strong>
</a>
<small><em>Posted on $date</em></small>
</p>
<p>
$description
</p>
HTML;
}
I want to get the URL of the image in . The XML document tree is as follow:
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>
<![CDATA[ The Star Online Business Highlights ]]>
</title>
<link>/TheStar/Website</link>
<description>...</description>
<image>...</image>
<language>en</language>
<item>
<guid isPermaLink="false">{F88B27DD-24FB-4807-941F-070D772B7586}</guid>
<link>
http://www.thestar.com.my/business/business-news/2017/10/24/top-glove-says-not-buying-adventa-nor-supermax/
</link>
<title>
<![CDATA[ Top Glove says not buying Adventa nor Supermax ]]>
</title>
<description>
<![CDATA[KUALA LUMPUR: Top Glove, which has allocated about RM1bil to expand via mergers, has denied news reports the target companies are Adventa Bhd and Supermax Corporation Bhd.]]>
</description>
<pubDate>Tue, 24 Oct 2017 13:17:18 +08:00</pubDate>
<enclosure url="http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1&w=0&h=0&" length="" type="image/jpeg"/>
<media:content url="http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1&w=0&h=0&" type="image/jpeg">
<media:description>
<![CDATA[ ]]>
</media:description>
</media:content>
<section>
<![CDATA[ Business ]]>
</section>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
</channel>
As there is multiple item and I want to make it a loop, I tried:
foreach($xml->channel->item as $news) {
$media = $news->media->children('http://search.yahoo.com/mrss/');
echo ($media->content);
}
and also
foreach($xml->channel->item as $news) {
$media = $news->children('http://search.yahoo.com/mrss/');
echo ($media->content);
}
but both are seems failed. What is the right method?
The $media variable is of type SimpleXMLElement.
What you could do is loop your $media variable in a foreach and then get your url from the attributes.
For example (using simplexml_load_string with additional Libxml parameters to load your example xml:
$source = <<<SOURCE
//Your example xml here
SOURCE;
$xml = simplexml_load_string($source, "SimpleXMLElement", LIBXML_NOERROR|LIBXML_ERR_NONE|LIBXML_ERR_FATAL);
foreach($xml->channel->item as $news) {
$media = $news->children('http://search.yahoo.com/mrss/');
foreach($media as $child) {
echo $child->attributes()->url;
}
}
Will result in:
http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1=0=0
$xml = new SimpleXMLElement($xml, LIBXML_NOERROR|LIBXML_ERR_NONE|LIBXML_ERR_FATAL);
foreach ($xml->xpath("//media:content") as $node)
{
var_dump ((string) $node["url"]);
}
Structure of my RSS from http://rss.cnn.com/rss/edition.rss is:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://rss.cnn.com/~d/styles/itemcontent.css"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title><![CDATA[CNN.com - RSS Channel - Intl Homepage - News]]></title>
<description><![CDATA[CNN.com delivers up-to-the-minute news and information on the latest top stories, weather, entertainment, politics and more.]]></description>
<link>http://www.cnn.com/intl_index.html</link>
...
<item>
<title><![CDATA[Russia responds to claims it has damaging material on Trump]]></title>
<description><![CDATA[The Kremlin denied it has compromising information about US President-elect Donald Trump, describing the allegations as "pulp fiction".]]></description>
<link>http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html</link>
<guid isPermaLink="true">http://www.cnn.com/2017/01/11/politics/russia-rejects-trump-allegations/index.html</guid>
<pubDate>Wed, 11 Jan 2017 14:44:49 GMT</pubDate>
<media:group>
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-super-169.jpg" height="619" width="1100" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-large-11.jpg" height="300" width="300" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-large-gallery.jpg" height="552" width="414" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-video-synd-2.jpg" height="480" width="640" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-live-video.jpg" height="324" width="576" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-vertical-gallery.jpg" height="360" width="270" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-story-body.jpg" height="169" width="300" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-t1-main.jpg" height="250" width="250" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-assign.jpg" height="186" width="248" />
<media:content medium="image" url="http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-hp-video.jpg" height="144" width="256" />
</media:group>
</item>
...
</channel>
</rss>
If you parse this XML with simplexml like this:
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$rssjson = json_encode($rss);
$rssarray = json_decode($rssjson, TRUE);
you will see that <media:content> is simply missing in $rssarray items. So I found a tutorial with "namespace" solution. However, in the example author is using:
foreach ($xml->channel->item as $item) { ... }
but I am using (cannot use foreach for some reasons):
$rssjson = json_encode($rss);
$rssarray = json_decode($rssjson, TRUE);
So I modified the solution for my case like this:
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$namespaces = $rss->getNamespaces(true); // get namespaces
$rssjson = json_encode($rss);
$rssarray = json_decode($rssjson, TRUE);
if (isset($rssarray['channel']['item'])) {
foreach ($rssarray['channel']['item'] as $key => $item) {
$media_content = $rss->channel->item[$key]->children($namespaces['media']);
foreach($media_content as $tag) {
$tagjson = json_encode($tag);
$tagarray = json_decode($tagjson, TRUE);
}
}
}
But it does not work. For every item I get in $tagarray as a result an array with this structure:
Array(
'content' => array(
'0' => array(null),
'1' => array(null),
...
'11' => array(null),
)
)
It is an array with as many items as is the count of <media:content> tags, but every item is empty. I need to get an url attribute of every item. What am I doing wrong and getting an empty array?
Tags are actually empty:
<media:content ... />
^^
Information is contained in attributes, which can be fetched with SimpleXMLElement::attributes(), e.g.:
$rss = simplexml_load_file($url, null, LIBXML_NOCDATA);
$namespaces = $rss->getNamespaces(true);
$media_content = $rss->channel->item[0]->children($namespaces['media']);
foreach($media_content->group->content as $i){
var_dump((string)$i->attributes()->url);
}
I suspect the problem comes from the JSON trick. SimpleXML generates all its classes and properties dynamically (they aren't regular PHP classes), what means that you can't fully rely on standard PHP features like print_r() or json_encode(). This gets illustrated if you insert this in the above loop:
var_dump($i, json_encode($i), (string)$i->attributes()->url);
object(SimpleXMLElement)#2 (0) {
}
string(2) "{}"
string(91) "http://i2.cdn.turner.com/cnnnext/dam/assets/161115120658-trump-putin-t1-tease-super-169.jpg"
...
I had requirement to aggregate RSS news feeds from different source which had images tags in different formats so I used below code:
//Sample Feed 1: https://www.hindustantimes.com/rss/topnews/rssfeed.xml
//Sample Feed 2: https://economictimes.indiatimes.com/rssfeedsdefault.cms
$feed=$_GET['feed'];
$rss = simplexml_load_file($feed);
$namespaces = $rss->getNamespaces(true);
echo '<strong>'. $rss->channel->title . '</strong><br><br>';
foreach ($rss->channel->item as $item) {
$media_content = $item->children($namespaces['media']);
foreach($media_content as $i){
$imageAlt = (string)$i->attributes()->url;
}
echo "Link: " . $item->link ."<br>";
echo "Title: " . $item->title ."<br>";
echo "Description: " . $item->description ."<br>";
echo "PubDate: " . $item->pubDate ."<br>";
echo "Image: " . $item->image ."<br>";
echo "ImageAlt: " . $imageAlt ."<br>";
echo "<br><br>";
}
I know child elements have been discussed a lot, but I've gone through the helpful answers to related questions and can't seem to get it working (new to coding, so bear with me).
Here's what I'm working with:
rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:bc="http://www.brightcove.tv/link" xmlns:dcterms="http://purl.org/dc/terms/" version="2.0">
<channel>
<title>Search Videos By Criteria</title>
<link>...</link>
<description/>
<copyright>Copyright 2014</copyright>
<lastBuildDate>Thu, 25 Sep 2014 13:29:49 -0700</lastBuildDate>
<generator>http://www.brightcove.com/?v=1.0</generator>
<item>
<title>5 best guards in Lakers history</title>
<link/>
<description>...</description>
<guid>video3805826070001</guid>
<pubDate>Thu, 25 Sep 2014 05:11:39 -0700</pubDate>
<media:content duration="121" medium="video" type="video/mp4" url="http://videos.usatoday.net/Brightcove2/29906170001/2014/09/29906170001_3805837947001_5-BEST-GUARDS-IN-LAKERS--HISTORY-final.mp4?videoId=3805826070001"/>
<media:group>...</media:group>
<media:keywords>jerry west,derek fisher,Gail Goodrich,losangeleslakers,SMGV,USA Today Sports,Kobe Bryant,video big board,sports,basketball,lakers,magic johnson,nba
</media:keywords>
<media:thumbnail height="90" url="http://videos.usatoday.net/Brightcove2/29906170001/2014/09/29906170001_3805822421001_Screen-Shot-2014-09-25-at-8-06-28-AM.jpg?pubId=29906170001" width="120"/>
<media:thumbnail height="360" url="http://videos.usatoday.net/Brightcove2/29906170001/2014/09/29906170001_3805709286001_Screen-Shot-2014-09-25-at-8-06-28-AM.jpg?pubId=29906170001" width="480"/>
<bc:titleid>3805826070001</bc:titleid>
<bc:duration>121</bc:duration>
<dcterms:valid/>
<bc:accountid>44854217001</bc:accountid>
</item>
I'm using the following SimpleXML_Parser script to pull most of the info out that I need:
<?php
$html = "";
$url = "http://api.brightcove.com/services/library?command=search_videos&any=tag:NBA&output=mrss&media_delivery=http&sort_by=CREATION_DATE:DESC&token=NU-nMdtzfF8z9NNinlAgM4c9S-9BBfKpm6gFISdwyk-AnQ84efFBbQ..";
$xml = simplexml_load_file($url);
for($i = 0; $i < 80; $i++){
$title = $xml->channel->item[$i]->video;
$link = $xml->channel->item[$i]->link;
$title = $xml->channel->item[$i]->title;
$pubDate = $xml->channel->item[$i]->pubDate;
$description = $xml->channel->item[$i]->description;/* The code below starting with $html is where you setup how the parsed data will look on the webpage */
$html .= "<div><h3>$title</h3><br/>$description<p><br/>$pubDate<p><br/>$link<p><br/>$titleid<p><br/></div><iframe width='580' height='360' src='http://link.brightcove.com/services/player/bcpid3742068445001?bckey=/*deleted API key&bctid=$titleid' frameborder='0'></iframe><hr/>";}
echo $html;/* tutorial for this script is here https://www.youtube.com/watch?v=4ZLZkdiKGE0 */?>
What I need to be able to parse out of the feed is the string of number assigned to "titleid"
I have tried adding in variations on approaches for pulling out child elements, such as:
$titleid = $xml->children(‘media’, true)->div->children(‘bc’, true)->div[$i]->titled;
But not having any luck. I'm sure it's something obvious to a seasoned developer, but again, I'm a newbie.
Any suggestions?
Thanks for any help!
To parse MRSS properly you need first to put the getNamespaces to true.
Then select the namespace $xml->channel->item[$i]->children($namespaces['bc']) finaly you can extract the wanted value from it in your case id
<?php
$html = "";
$url = "http://api.brightcove.com/services/library?command=search_videos&any=tag:NBA&output=mrss&media_delivery=http&sort_by=CREATION_DATE:DESC&token=NU-nMdtzfF8z9NNinlAgM4c9S-9BBfKpm6gFISdwyk-AnQ84efFBbQ..";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces
for($i = 0; $i < 80; $i++){
$title = $xml->channel->item[$i]->video;
$link = $xml->channel->item[$i]->link;
$title = $xml->channel->item[$i]->title;
$pubDate = $xml->channel->item[$i]->pubDate;
$description = $xml->channel->item[$i]->description;
$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
echo $title_group .'<br>';
}
I want to access the 'url' attribute in the media:content element below. I am pretty sure this gives me the media:content, but I can not seem to get the url (see what I tried below):
$theContent = $item->children('media', true)->content;
xml:
<item>
<media:content type="image/jpeg" url="my url" />
</item>
I have tried variations:
$theURL = $item->children('media', true)->content['url'];
and
$mediaItem=$item->children('media', true)->content;
$contentItem=$mediaItem->children('content', true);
$url = $contentItem['url'];
No luck. ??
Complete working code with output:
<?php
$xml = '<item>
<media:content type="image/jpeg" url="my url" />
</item>';
$theContent = #new SimpleXMLElement($xml);
$attributes = $theContent->content->attributes();
echo $attributes['url']; //outputs: my url
?>
References: SimpleXML Attributes