PHP Last.fm Get Largest Image for an Artist - php

I have the following code
<?php
$xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Metallica&api_key=b25b959554ed76058ac220b7b2e0a026");
$artistTag= $xml->artist->children();
$largeImage = $artistTag[7];
echo '<img src="'.$largeImage.'" />';
?>
This will target the 7th node - however the 7th node might not exist so this won't work. Is there anyway to specifically target the large, extralarge or mega nodes?
Example XML
<lfm status="ok">
<artist>
<name>Metallica</name>
<mbid>65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab</mbid>
<url>http://www.last.fm/music/Metallica</url>
<image size="small">
http://img2-ak.lst.fm/i/u/34s/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="medium">
http://img2-ak.lst.fm/i/u/64s/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="large">
http://img2-ak.lst.fm/i/u/174s/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="extralarge">
http://img2-ak.lst.fm/i/u/300x300/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="mega">
http://img2-ak.lst.fm/i/u/c14fdad46a5c423f86a683501c163c99.png
</image>
<image size="">
http://img2-ak.lst.fm/i/u/arQ/c14fdad46a5c423f86a683501c163c99.png
</image>
So if mega doesn't exist, go for extralarge, if that doesn't exist, go to large etc

<?php
$xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Metallica&api_key=b25b959554ed76058ac220b7b2e0a026");
$largeImage = $xml->xpath('/lfm/artist/image[#size="mega"]')[0];
echo '<img src="'.$largeImage.'" />';
?>

This is possibly a duplicate of : SimpleXML: Selecting Elements Which Have A Certain Attribute Value
Here is what I would think for your particular case:
<?php
$xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=Metallica&api_key=b25b959554ed76058ac220b7b2e0a026");
$largeImage = $xml->xpath('/lfm/artist/image[#size="mega"]')[0];
echo '<img src="'.$largeImage.'" />';
?>

Related

php xml xpath how to extract desired match

I have question on how to access SourceUrl for image with width=400
images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198
By default it is showing me image with width=100 and somehow my xpath syntax is not picking up 400
<?php
$string = <<<XML
<imageList>
<image available="true" height="100" width="100">
<sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-100x100-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL>
</image>
<image available="true" height="200" width="200"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-200x200-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="300" width="300"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-300x300-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="400" width="400"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image>
<image available="true" height="569" width="500"><sourceURL>images/di/47/6b/77/454430384d6d324b413332544a695675313851-500x569-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198</sourceURL></image></imageList>
XML;
$xml = simplexml_load_string($string);
$result = $xml->xpath("//image[#height='400']/sourceURL");
?>
Your xpath is all right, but the XML is not valid, see http://www.xmlvalidation.com
The & within your links will be parsed as start of a character entity --> error.
solution: exclude the text within <sourceURL> from parsing with <![CDATA[...]]>:
$x = <<<XML
<imageList>
<image available="true" height="100" width="100">
<sourceURL>
<![CDATA[images/di/47/6b/77/454430384d6d324b413332544a695675313851-100x100-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198]]>
</sourceURL>
</image>
...
</imageList>
XML;
$xml = simplexml_load_string($x);
$result = $xml->xpath("//image[#height='400']/sourceURL")[0];
echo $result;
output:
images/di/47/6b/77/454430384d6d324b413332544a695675313851-400x400-0-0.jpg?p=p2.7f19fe93a466ae45afab&a=1&c=1&l=7000610&r=1&pr=1&lks=43998&fks=35198
see it working: http://codepad.viper-7.com/31PLr3
more on CDATA: What does <![CDATA[]]> in XML mean?

Feching the values from xml on the condition check

I got a problem
<?php
$movie_name = 'Dabangg';
$url = sprintf(
'http://api.themoviedb.org/2.1/Movie.search/en/xml/mapi/%s',
rawurlencode(trim($movie_name))
);
$xml = simplexml_load_file($url);
if (!$xml)
{
throw new RuntimeException(sprintf("Failed to open XML from URL '%s'.", $url));
}
$movies = $xml->movies->movie;
foreach ($movies as $movie)
{
$movie_id = $movie->id;
if ($movie_id && is_int($movie_id))
{
break;
}
}
echo $movie_id;
The above code I am using to fecth the movieid . Now I realized that when I search for a particular movie it fetches two movies result
<OpenSearchDescription xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">
<opensearch:Query searchTerms="Dabangg"/>
<opensearch:totalResults>2</opensearch:totalResults>
<movies>
<movie>
<score/>
<popularity>3</popularity>
<translated>true</translated>
<adult>false</adult>
<language>en</language>
<original_name>Dabangg</original_name>
<name>Dabangg</name>
<alternative_name/>
<type>movie</type>
<id>44425</id>
<imdb_id>tt1620719</imdb_id>
<url>http://www.themoviedb.org/movie/44425</url>
<votes>6</votes>
<rating>5.6</rating>
<certification>PG-13</certification>
<overview>
Set in Uttar Pradesh, Dabangg is the story of a corrupt police officer played by Salman Khan and highlights the flaws and loopholes in the system. The film primarily deals with the unlawful practices in the states of UP and Bihar.
</overview>
<released>2010-09-10</released>
<images>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="thumb" width="92" height="138" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="w154" width="154" height="231" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="cover" width="185" height="278" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="w342" width="342" height="513" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="mid" width="500" height="750" id="4ea685f034f8633bdc00b45f"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/lfI4ihj4qa8Obyst9miTSNzHCqF.jpg" size="original" width="1000" height="1500" id="4ea685f034f8633bdc00b45f"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="thumb" width="300" height="169" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="poster" width="780" height="439" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="w1280" width="1280" height="720" id="4ea6860234f8633bdc00b477"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/sNoWUZD4S3C4gqWVwvifuiwsJ4m.jpg" size="original" width="1920" height="1080" id="4ea6860234f8633bdc00b477"/>
</images>
<version>800</version>
<last_modified_at>2013-04-09 03:03:56 UTC</last_modified_at>
</movie>
<movie>
<score/>
<popularity>3</popularity>
<translated>true</translated>
<adult>false</adult>
<language>en</language>
<original_name>Dabangg 2</original_name>
<name>Dabangg 2</name>
<alternative_name>沙曼罕之爆裂警官 2</alternative_name>
<type>movie</type>
<id>147405</id>
<imdb_id>tt2112131</imdb_id>
<url>http://www.themoviedb.org/movie/147405</url>
<votes>2</votes>
<rating>5.8</rating>
<certification>NR</certification>
<overview>
Dabangg 2 is an upcoming Bollywood action film directed and produced by Arbaaz Khan under the banner of Arbaaz Khan Productions.It is a sequel to the 2010 film Dabangg and is written by Dilip Shukla. Story is set in the city of Kanpur
</overview>
<released>2012-12-21</released>
<images>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w92/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="thumb" width="92" height="130" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w154/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="w154" width="154" height="217" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="cover" width="185" height="261" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w342/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="w342" width="342" height="483" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w500/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="mid" width="500" height="706" id="514727ab760ee3215200fe27"/>
<image type="poster" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/5VW8AEfj4r3bu9MtvwvcbWAN53O.jpg" size="original" width="1156" height="1632" id="514727ab760ee3215200fe27"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="thumb" width="300" height="169" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w780/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="poster" width="780" height="439" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w1280/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="w1280" width="1280" height="720" id="51472838760ee3216f010a1c"/>
<image type="backdrop" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/rWGMdyTydjXXh9YphuU5gQ7wJ8X.jpg" size="original" width="1920" height="1080" id="51472838760ee3216f010a1c"/>
</images>
<version>2</version>
<last_modified_at>2013-04-28 21:28:49 UTC</last_modified_at>
</movie>
</movies>
</OpenSearchDescription>
There are two movie Ids in this result. The Id which I want is 147405 but I am only getting the Id 44425. How can I get the other movie ID.
If we see the movie name I searched for <name>Dabangg 2</name> bit it send the <name>Dabangg</name> also . How can I make a check of name and take that id from the xml.
If you want to search for the exact term like
$movieName = 'Dabangg 2';
You need to wrap it in double-quotes:
$apiKey = '30f44b6ef9472d414e50d2acaa058b60';
$url = sprintf(
'http://api.themoviedb.org/2.1/Movie.search/en/xml/%s/"%s"',
$apiKey,
rawurlencode(trim($movieName))
);
Which will allow you to obtain the ID you're looking for immediately:
$xml = simplexml_load_file($url);
$movie = $xml->movies->movie;
echo $movieName, ': ', $movie->id; # Dabangg 2: 147405
break ends execution of current while. then do not use it, because after first finding match, it ends the while loop

PHP, DOM - remove first several nodes

The xml file looks like this:
<newVotes>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_524700002.jpg" id="1"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5317gg.jpg" id="2"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5393.jpg" id="3"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_2299.jpg" id="4"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4977.jpg" id="5"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4977BW.jpg" id="6"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4914.jpg" id="7"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="flowergirl.jpg" id="8"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5393.jpg" id="9"/>
<image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_2299.jpg" id="10"/>
</newVotes>
I don't know how many "image" nodes will contain my xml, and how many of them goes to remove (it's dynamic amount variable comes from flash module) My question is how to delete for example first 5 "image" nodes in xml, using PHP?
Especialy added "id" nodes because I thought I can delete them using Xpath somehow, but with no lucky...
Thanks for any help.
Artur.
Done... It was pretty simple:
$filesLimit = $_POST['_limit'];
$rateElement = $news_dom->getElementsByTagName('image');
$numberOfFiles = $rateElement->length;
$m = $numberOfFiles - $filesLimit + 1;
if($numberOfFiles>=$filesLimit){
for ($i = 0; $i < $m; $i++) {
$nodesToRemove = $rateElement->item(0)->parentNode->removeChild($rateElement->item(0));
}
}

Storing the content of an xml child node based on a parent node's attribute in php

I'm trying to display the biggest image url returned from an xml result. So far the largest returned is 400 high so I hardcoded 400 in. If possible I would like to select just the largest in case in the future I get results that don't have a 400 height image in them.
I've tried
$x = file_get_contents($url);
$xml = simplexml_load_string($x);
$imageURL=$xml->categories->category->items->product->images->image[#height='400']->sourceURL;
Which gives me "syntax error, unexpected '=', expecting ']'".
And I also tried:
$imageURL= $xml->xpath("/categories/category/items/producct/images/image[#height='400']/sourceURL");
But got a bad link.
Here is the XML:
<images>
<image available="true" height="100" width="100">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="200" width="200">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="300" width="300">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="400" width="400">
<sourceURL>
Someurl.com
</sourceURL>
</image>
<image available="true" height="399" width="400">
<sourceURL>
Someurl.com
</sourceURL>
</image>
</images>
Any ideas?
->image[#height='400'] is a direct PHP array reference. This'd be interpreted as supressing errors (#) on a defined() constant (height), and trying to set its value via an assignment ='400'.
For your xpath version, remember that an xpath query returns a DOMNodeList, not an actual DOMElement. To get the URLs you need from the query results, you have to ierate over the node list:
$nodes = $xpath->query(...) {
foreach($nodes as $node) {
$url = $node->nodeValue;
}
Below code might help...
$xmlSQLProcedures = new DOMXPath($xmlSQLProcedures);
$strProcedureName = $xmlSQLProcedures->query("//SQLProcedure[#ID='$sSQLProcedureID']")->item(0)->nodeValue;
$nodeParameters = $xmlSQLProcedures->query("//SQLProcedure[#ID='$sSQLProcedureID']/Parameters/Parameter");
$ParamCount = $nodeParameters->length-1;
for ($i=0;$i<=$ParamCount;$i++) {
echo $nodeParameters->item($i)->getAttribute("Name").'<br>';
}
<?xml version="1.0" encoding="UTF-8"?>
<SQLProcedures>
<!-- ********** FOR KEYWORD IN LOCAL LANGUAGE ************* -->
<SQLProcedure ID="001070001">
<Name>P_ManipulateKeywordsInLL</Name>
<Parameters>
<Parameter Name="LanguageId"/>
<Parameter Name="KeywordId"/>
<Parameter Name="KeywordInLL"/>
<Parameter Name="ActionFor"/>
<Parameter Name="KeywordInLLId"/>
<Parameter Name="Keyword"/>
<Parameter Name="KeywordList"/>
<Parameter Name="SessionId"/>
<Parameter Name="WarehouseId"/>
</Parameters>
</SQLProcedure>
</SQLProcedures>

Get image from XML and display with PHP?

I'm trying to use an API with the following XML:
<movies>
<movie>
<images>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-mid.jpg" size="mid" width="500" height="741" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-cover.jpg" size="cover" width="185" height="274" id="4bc91de5017a3c57fe00bb7a"/>
</images>
</movie>
</movies>
Can someone give me an example of the PHP code I should use to get the image url where size="cover"?
Thanks.
SimpleXML can do this quite, for lack of a better word, simply:
$xml = new SimpleXMLElement($str);
$xpath = $xml->xpath("/movies/movie/images/image[#size = 'cover']");
echo $xpath[0]['url'];
Load the xml with a XML Parser, DOMDocument, SimpleXML etc.
http://se.php.net/manual/en/refs.xml.php
Then you can use XPath to select the image.
http://www.w3schools.com/xpath/xpath_syntax.asp
XPath to grab movie with attribute size = cover
/movies/movie/images/image[#size=cover]
Looks like a good tutorial: http://ditio.net/2008/12/01/php-xpath-tutorial-advanced-xml-part-1/
<?php
$string = <<<XML
<?xml version='1.0'?>
<movies>
<movie>
<images>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-original.jpg" size="original" width="675" height="1000" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-mid.jpg" size="mid" width="500" height="741" id="4bc91de5017a3c57fe00bb7a"/>
<image type="poster" url="http://cf1.imgobject.com/posters/b7a/4bc91de5017a3c57fe00bb7a/i-am-legend-cover.jpg" size="cover" width="185" height="274" id="4bc91de5017a3c57fe00bb7a"/>
</images>
</movie>
</movies>
XML;
$xml = simplexml_load_string($string);
foreach($xml->movie->images->image as $image) {
if(strcmp($image['size'],"cover") == 0)
echo $image['url'];
}
?>
$xml = simplexml_load_string($string2);
foreach($xml->movie->images->image as $image) {
if(strcmp($image['size'],"cover"))
// echo $image['url'];
?>
" width="200px" height="100px">

Categories