LATEST POST - My first Arduino Circuit
Posted In Tutorials, Web Design on April 9th, 2011
PixelGrafters is currently under going massive optimization and development and I will be releasing version 3.0 hopefully with a mobile version of the site. Although that may be released as an extra feature in version 3.5. Im going to show you a snippet here of how im optimising the code for grabbing search results from themoviedb.org
Previously PixelGrafters was using the PHP cURL() function to return search results from themoviedb.org in XML and then provide the massive amount of filtering needed to get rid of the junk submitted to its open and free to use database. The filtering is necessary to provide the user a much more user friendly view of information about a requested movie. Also it acts similar to basic AI allowing PixelGrafters to auto select a movie for you based on your search terms.
Now though I have improved the method for returning results making PixelGrafters much faster and it will also make it nicer to browse when the site is finished. It seems before I was taking the long and hard way of doing it when there was a much more simpler option I hadn’t thought of. Just incase anybody else is wondering how to pull search results from themoviedb.org fairly simply then heres some PHP code below. Ill explain it first.
Briefly this code is going to give you the thumbnail image for each movie in the search results from themoviedb.org of a query passed through the url like so… www.mydomain.com/myquery.
$URI = $_SERVER["REQUEST_URI"];
$URI_VARS = explode("/", $URI);
$query = $URI_VARS[1];
$APIKey = "Sorry I cant give my API key away";
$file = 'http://api.themoviedb.org/2.1/Movie.search/en/xml/'
. $APIKey . '/' . $query;
if(!$xml = simplexml_load_file($file))
{
exit('Failed to open '.$file);
}
$movieCount=0;
foreach($xml->movies->movie as $movie)
{
$imageCount=0;
foreach($xml->movies->movie[$movieCount]->images->image as $image)
{
if ($image['size'] == 'cover')
{
echo '<img src="';
echo $image['url'];
echo '" ></img>';
echo '<br/>';
}
$imageCount++;
}
$movieCount++;
}In Detail what this does is it will request the URI (the string of data after the .com or .co.uk etc), split it up to get the data after the forward-slash and treat that data as your search query. Then its going to set the remote location to themoviedb.org’s API with your API key and the query you passed. Then it will load the XML from that location into a simplexml object. Once successfully doing that it will loop through each movie in the search results and then each image in each movie and each time it finds an image of the size “cover” in the XML it will echo the image to the page.
I hope thats of any use to anybody trying to code something funky using themoviedb.org’s API. Enjoy and the new PixelGrafters will be out soon.