How to parse this <media:thumbnail url=""> using PHP - php

I am not able to parse this type of code:
<media:thumbnail url="http://images.indianexpress.com/2015/07/man-u-reuters_t.jpg?w=450" /> <media:content url="http://images.indianexpress.com/2015/07/man-u-reuters_t.jpg?w=450" medium="image">
When I am using this code,
<?php
$html = "";
$xml = simplexml_load_file("sports.xml");
foreach ($xml->channel->item as $news)
{
// echo $news->title;
$ns_media = $news->children("http://search.yahoo.com/mrss/");
echo $ns_media->content->title . "<br/>"; // Displays "<media:content>"
}
I am getting output as:
Man-u-reuters_t
IanBellReuters-T
dutee-body2
Dutee-T
duleeptrophy-T
EnglandReuters-T
AzharAliAP-T
PakistanAP-T
AndersonReuters-T
KarunNairFile-T
ViratKohliPTI-T
kaneriaIEFile-M
PlatiniAP-T
asheslives
IndiaAustraliaT
ChelseaBarcelonaAPT
BradHaddinReutersT
ankeet-t
Arcehry-T
SardarSinghT
DuteeChandFB-T
CliveRice-T
ChrisRogersAP-T
ClarkeReuters-T
AlastairCookReuters-T
GangulyReuters-T
SachinTendulkarPTI-T
GangulyPTI-T
CliveRiceAP-T
ArcheryIEfile-T
PujaraPTIT
AshtonAgarTPTI
PeterNevillReuters-T
APJAbdulKalamT
IanBellReutersT
ChrisRogersAPT
hockey-small
dutee-chand-mi
DuteeChandT
mathew-small
ViratKohliPTIT
Sushil-Kumar-feat
Pro-Wrestling-League-feat
AnuragThakurT
SSreesanthPTIT
LalitModiT
Anirban-Lahiri-f
India-hockey-f
srk_express-file_t
Chris-Gayle-f
Viru_pti_t
Sergio-Romero_reuters_t
SachinPTI-T
SREESANTH_PTI_T
SSreesanthPTI-T
Srinivasan-T
TourdeFranceReuters-T
SriLankaReuters-T
cricket
SreesanthPTI-T
VettelReuters-T
WarneReuters-T
MarkWoodReuters-T
Misbah-ul-Haq-F
S-Sreesanth-f
yUVRAJ-_PTI_T
Sreesanth-f
abd_facebook_t
Yuvraj-Singh-f
Stuart-Broad-m
Chris-Rogers-f
Manchester-_reuters_t
kabaddi-thumb
Oltmans-T
ajit-thumb
sreesanth-thumb
Paul-Van-Ass_t
sreesanth1-thumb
Sreesanth_pti_t
Rahul-Dravid_pti_t
Amit-Mishra_pti-t
Lewis-Hamilton-F
Roelant-Oltman-MM
S-Sreesanth-f
bcci-thumb
Sourav-Ganguly_pti_t
SreesanthFileT
Bangladesh-f
Lewis-Hamilton-f
Sreesanth
Pakistan-f
Dale-Steyn_twitter_t
Usain-Bolt-f
Dhoni-f
Gayle_reuters_t
Usain-usa_t
Kohli-m
golf-thumb
Hockey-thumb
BCCI-thumb
felix-thumb
Didier-Drogba-f
VijenderSinghPTIT
RonaldoReutersT
Van-Ass-f
PragyanOjhaLT
SergioPerezAPT
IndiahockeyT
Saina-Nehwal-feat
MariaSharapovaT
Chung-Mong-joon-feat
Afganistan-feat
Bangladesh-feat
ShoaibSaniaLT
SureshRainaAPT
jaglan-480
mishra-thumb
oltmans-thumb
England-team-feat
Ojha-t
D-Steyn-main
AxarPatelT
BCCI-feat
Anurag-Thakur_t
SureshRainaT
Usain-Bolt-feat
MSDHoniT
ViratKohliPTIT
AmitMishraT
Anurag-Thakur-feat
ViratKohliFileTt
TamimIqbalAPT
ViratKohliT
raviICC-T
YurajSingh-T
ABdevilliers-T
BangladeshAP-t
DhoniAP-T
ShehzadReuters-T
ViratKohli-T
RahulPujaraBCCI-T
dhyan
YounisAP-T
Sangram-Singh-feat
ISl-feat
MahmudullahAP-T
Virat-Kohli_pti_m
Usain-Bolt-feat
Salman-Khan-feat
Yasir-Shah-feat
VVS-laxman_pti_t
Ashwin_facebook_t
Patil_pti_t
Virat-Kohli-ap-t
Sania-Mirza_facebook_t
Bairstow-ap-t
bcci-thumb
Virbhadra-Singh-thumb
MohitChhillar
VikasKrishan-T
yuri-small
IndiaHockeyVanAssPTIT
Sarfaraz-T
Cricket-T
Kevin-Pietersen_AP_2-M
SterlingReuters-T
JonnyBairstowReuters-T
Matthew-Wade-feat
BianchifuneralReuters-T
LehmannReuters-T
Praful-Patel-feat
mustafizurAP-T
Cheteshwar-Pujara--feat
Varun-Aaron-feat
Pele-feat
EdenGardensFileT
mickfanningT
RahulDravidT
SachinTendulkarWarneT
Jeffrey-WebbAPT
ChrisRogersReutersT
ZachJohnsonReutersT
ZachJohnsonReuters-T
JKCA
AnuragThakurPTLT
Paltan-T
Van-Ass_t
hockey
YounisKhanAP-T
RahulDravidPTI-T
UsmanAP-T
FIFAReuters-T
BlatterReuters-T
SaniaMirzaPTI-T
Harbhajan_singh_reuters_t
Central-Zone-feat
Rajiv-Shukla-feat
Paul-Van-Ass_t
Ganguly_pti_t
Sri-Lanka-Riots
I am not able to parse the <media:content url="">.
Stack Overflow question How to get media:content with SimpleXML is not working.

I wrote this a while back, to parse an RSS feed from the BBC...
$rssItems = $xml->channel->item;
foreach($rssItems as $rssItem){
if(is_array(($rssItem->xpath('media:thumbnail')))){
// If there are.. ..get them as an Array()
$mediaArray = $rssItem->xpath('media:thumbnail');
// Select the last Array item
$media = end($mediaArray);
// Build some variables to use in a string
$src = $media->attributes()->url;
$width = $media->attributes()->width.'px';
// Build a HTML string using the variables
$imageString = "<img src='$src' width='$width' />";
}
}

Related

Get PHP array from XML responce with namespaces

I receive an XML repsonce from a GEO service (PDOK). The $responce->raw_body contains this XML structure:
<xls:GeocodeResponse xmlns:xls="http://www.opengis.net/xls" xmlns:gml="http://www.opengis.net/gml">
<xls:GeocodeResponseList numberOfGeocodedAddresses="1">
<xls:GeocodedAddress>
<gml:Point srsName="EPSG:28992">
<gml:pos dimension="2">121299.73296672151 487003.8972524117</gml:pos>
</gml:Point>
<xls:Address countryCode="NL">
<xls:StreetAddress>
<xls:Street>Rokin</xls:Street>
</xls:StreetAddress>
<xls:Place type="MunicipalitySubdivision">Amsterdam</xls:Place>
<xls:Place type="Municipality">Amsterdam</xls:Place>
<xls:Place type="CountrySubdivision">Noord-Holland</xls:Place>
</xls:Address>
</xls:GeocodedAddress>
</xls:GeocodeResponseList>
</xls:GeocodeResponse>
How can I access the elements in here. For example I want an PHP array to access the element 121299.73296672151 487003.8972524117
to grap the coordinates.
And also the other elements. I used SimpleXML parser but I recieve always null.
I think it has someting to do with the namespaces. But I have no clue how to solve this.
The responce is coming from:
$url = "http://geodata.nationaalgeoregister.nl/geocoder/Geocoder?zoekterm=xxxxx%20xx";
$response = \Httpful\Request::get($url)
->expectsXml()
->send();
$xml = new \SimpleXMLElement($response->raw_body);
print_r( $xml);
output:
SimpleXMLElement Object ( )
Any help is appriciated!
After some digging I found a solution for my problem. Its indeed by the namespaces and when using xpath after the namespace registration you can find the elements you need.
$xml = new \SimpleXMLElement($response);
$xml->registerXPathNamespace('xls', 'http://www.opengis.net/xls');
$xml->registerXPathNamespace('gml', 'http://www.opengis.net/gml');
foreach($xml->xpath('//xls:GeocodeResponseList') as $header)
{
$geocoordinates = $header->xpath('//gml:pos');
$street = (string) ($header->xpath('//xls:Street')[0]);
$place = (string) ($header->xpath('//xls:Place')[2]);
}
echo $geocoordinates[0]; // get the coordinates needs to split on space
echo "<br />";
echo $geocoordinates[0]['dimension']; // get the dimension attribute
echo "<br />";
echo $street;
echo "<br />";
echo $place;
Use "->expectsJson()" instead of "->expectsXml()". Then do this:
$array = json_decode($response->raw_body);
or if you use PHP 7.x: https://github.com/eddypouw/geodata-postal-api

Namespace in MRSS feed using simplexml PHP Script

Tried to research what I'm doing wrong here, but no luck so far. I want to pull the links and URLs in this MRSS feed using this script, but it's not working. Thought all I needed to do was use namespaces to get the child elements out, but no luck:
<?php
$html = "";
$url = "http://feeds.nascar.com/feeds/video?command=search_videos&media_delivery=http&custom_fields=adtitle%2cfranchise&page_size=100&sort_by=PUBLISH_DATE:DESC&token=217e0d96-bd4a-4451-88ec-404debfaf425&any=franchise:%20Preview%20Show&any=franchise:%20Weekend%20Top%205&any=franchise:Up%20to%20Speed&any=franchise:Press%20Pass&any=franchise:Sprint%20Cup%20Practice%20Clips&any=franchise:Sprint%20Cup%20Highlights&any=franchise:Sprint%20Cup%20Final%20Laps&any=franchise:Sprint%20Cup%20Victory%20Lane&any=franchise:Sprint%20Cup%20Post%20Race%20Reactions&any=franchise:All%20Access&any=franchise:Nationwide%20Series%20Qualifying%20Clips&any=franchise:Nationwide%20Series%20Highlights&any=franchise:Nationwide%20Series%20Final%20Laps&any=franchise:Nationwide%20Series%20Victory%20Lane&any=franchise:Nationwide%20Series%20Post%20Race%20Reactions&any=franchise:Truck%20Series%20Qualifying%20Clips&any=franchise:Truck%20Series%20Highlights&any=franchise:Truck%20Series%20Final%20Laps&any=franchise:Truck%20Series%20Victory%20Lane&any=franchise:Truck%20Series%20Post%20Race%20Reactions&output=mrss";
$xml = simplexml_load_file($url);
$namespaces = $xml->getNamespaces(true); // get namespaces
for($i = 0; $i < 50; $i++){ // will return the 50 most recent videos
$title = $xml->channel->item[$i]->title;
$link = $xml->channel->item[$i]->link;
$pubDate = $xml->channel->item[$i]->pubDate;
$description = $xml->channel->item[$i]->description;
$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$url = $xml->channel->item[$i]->children($namespaces['media'])->url;
$html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p>
<iframe width='480' height='270' src='http://link.brightcove.com/services/player/bcpid3742068445001?bckey=//my API token goes here &bctid=$titleid&autoStart=false' frameborder='0'></iframe><hr/>";/* this embed code is from the youtube iframe embed code format but is actually using the embedded Ooyala player embedded on the Campus Insiders page. I replaced any specific guid (aka video ID) numbers with the "$guid" variable while keeping the Campus Insider Ooyala publisher ID, "eb3......fad" */
}
echo $html;
?>
I take it this isn't the right approach:
$url = $xml->channel->item[$i]->children($namespaces['media'])->url;
What am I doing wrong here?
Thanks for any and all help!
MD
SimpleXML is deceptively named as it is more difficult to use than DOMDocument or the other PHP XML extensions. To get the URL, you'll need to access the url attribute of the media:content node:
<media:content duration="95" medium="video" type="video/mp4"
url="http://brightcove.meta.nascar.com.edgesuite.net/vod/etc"/>
Target the first <media:content> node using
$xml->channel->item[$i]->children($namespaces['media'])->content[0]
and get its attributes:
$m_attrs =
$xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
You can then access the url attribute:
echo "URL: " . $m_attrs["url"] . "\n";
Your code should thus be:
$titleid = $xml->channel->item[$i]->children($namespaces['bc'])->titleid;
$m_attrs = $xml->channel->item[$i]->children($namespaces['media'])->content[0]->attributes();
$url = $m_attrs["url"];
$html .= "<h3>$title</h3>$description<p>$pubDate<p>$link<p>Video ID: $titleid<p> (etc.)";

Extract attribute from simple XML element in PHP

Driving me bonkers- I've got a simple XML element, and I just want to extract the '_Code' attribute. How would I do it?
<?php
$responseCode = "<STATUS _Condition='FAILURE' _Code='0705' _Description='Search failed subject not found' />";
$xml = simplexml_load_string($responseCode);
print_r($xml);
$code = $xml=>#attributes=>_Code; // Parse error
$code = $xml['#attributes']['_Code']; // Returns blank
echo "CODE = ".(string)$code;
?>
CODE =
http://php.net/manual/en/function.simplexml-load-string.php
Use SimpleXMLElement::attributes()
$attrs = $xml->attributes();
$code = $attrs['_Code'];

how to parse xml with php

I am trying to read a weather feed from Yahoo to my site.
Using the code below I was able to print the xml.
What I really want to achieve now is to put the temperature and image in two different variables
$zipCode = "44418";
$url = "http://weather.yahooapis.com/forecastrss";
$zip = "?w=$zipCode";
$fullUrl = $url . $zip.'&u=c';
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$fullUrl);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
$returnYahooWeather = curl_exec($curlObject);
curl_close($curlObject);
print "yahooWeather". $returnYahooWeather;
//$temperature
//$image
You should go ahead and use simplexml or DOM to parse the XML and then you can iterate over the results. With SimpleXML this looks like this:
$zipCode = "44418";
$url = "http://weather.yahooapis.com/forecastrss";
$zip = "?w=$zipCode";
$fullUrl = $url . $zip.'&u=c';
$curlObject = curl_init();
curl_setopt($curlObject,CURLOPT_URL,$fullUrl);
curl_setopt($curlObject,CURLOPT_HEADER,false);
curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true);
$returnYahooWeather = curl_exec($curlObject);
curl_close($curlObject);
//print "here". $returnYahooWeather;
$xmlobj=simplexml_load_string($returnYahooWeather);
$res = $xmlobj->xpath("//yweather:condition");
$tmp = false;
while(list( , $node) = each($res)) {
$tmp = $node;
}
$attribs = $tmp->attributes();
print "Temperature [".$attribs['temp']."]";
I find it easiest to SimpleXML with PHP.
$xml = simplexml_load_string($returnYahooWeather);
echo $xml->Path->To->Temperature;
It's easy enough, and you can use XPath with SimpleXML :). There are other ways of parsing XML too, as previously mentioned DOMDocument is one of them.

SimpleXML - "Node no longer exists"

I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.
The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.
If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = strval($attrs['url']);
$videoobj[$i]['title'] = strval($media->group->title);
}
SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:
$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url'];
$videoobj[$i]['title'] = (string) $media->group->title;

Categories