Skip to content

Instantly share code, notes, and snippets.

@travisbell
Created September 11, 2009 04:24
Show Gist options
  • Save travisbell/185074 to your computer and use it in GitHub Desktop.
Save travisbell/185074 to your computer and use it in GitHub Desktop.
<?php
/**
* MoviedbComponent - API-Implementation for 'themoviedb.org'
* API-Dokumentation: http://api.themoviedb.org/2.0/docs/
*
* Usage:
* 1. Include this component in your controller
* |-> Example: public $components = array('Moviedb');
* 2. Request your API-Key and edit the component config below
* 3. Getting started to use the component
* |-> Example: $this->Moviedb->search(Transformers);
*
* @package : MoviedbComponent
* @author : Stephan Pohl <imac.pohl@web.de>
* @revision : 2
* @since : 21.08.2009
* @available : 21.08.2009
* @copyright : Stephan Pohl <imac.pohl@web.de>
* @licence : CC-by-NC <http://creativecommons.org/licenses/by-nc/3.0/legalcode>
*
*/
class MoviedbComponent extends Object {
/**
* CakePHP Internal Name
*/
public $name = 'Moviedb';
/**
* Component Settings
*/
public $config = array(
/**
* General Settings
*/
'general' => array(
/**
* Your API-Key
*
* You need to sign up at "http://www.themoviedb.org/account/signup"
* and request a key at "api@themoviedb.org"
*/
'apikey' => '',
/**
* Request-URL
*/
'url' => 'http://api.themoviedb.org/2.0/'
),
/**
* Provided API Methods
*/
'methods' => array(
'search' => 'Movie.search?',
'imdbLookup' => 'Movie.imdbLookup?',
'getInfo' => 'Movie.getInfo?'
)
);
/**
* The 'search' method is the easiest and quickest way to search for a movie.
* It is a mandatory method in order to get the movie id
* to pass to (as an example) the Movie.getInfo method.
*
* @access: public
* @param : string $title
* @return: array $results
*
*/
public function search($title) {
$url = $this->config['general']['url']
. $this->config['methods']['search']
. 'title=' . urlencode($title)
. '&api_key=' . $this->config['general']['apikey'];
$results = $this->getResults($url);
return $results;
}
/**
* The 'getInfo' method is used to retrieve specific information about a movie.
* Things like overview, release date, cast data, genre's, YouTube trailer link, etc..
*
* @access: public
* @param : int $id
* @return: array $results
*
*/
public function getInfo($id) {
$url = $this->config['general']['url']
. $this->config['methods']['getInfo']
. 'id=' . $id
. '&api_key=' . $this->config['general']['apikey'];
$results = $this->getResults($url);
return $results;
}
/**
* The 'imdbLookup' method is the easiest and quickest way to search for a movie based on it's IMDb ID.
* You can use 'imdbLookup' method to get the TMDb id of a movie if you already have the IMDB id
*
* @access: public
* @param : string $imdbId
* @return: array $results
*
*/
public function imdbLookup($imdbId) {
$url = $this->config['general']['url']
. $this->config['methods']['imdbLookup']
. 'imdb_id=' . $imdbId
. '&api_key=' . $this->config['general']['apikey'];
$results = $this->getResults($url);
return $results;
}
/**
* The 'getResults' method getting the requested data and parse the returned XML
* in an Array
*
* @access: private
* @param : string $url
* @return: array $result
*
*/
private function getResults($url) {
if($fp = fopen($url, 'r')) {
ob_start();
fpassthru($fp);
$result = ob_get_contents();
ob_end_clean();
}
App::import('Xml');
$file = $result;
$parsed_xml =& new XML($file);
$result = Set::reverse($parsed_xml);
return $result;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment