A wrapper for IMDB API on http://www.imdbapi.com.
Requirements ¶
- Yii 1.1.
- CURL or fopen enabled in server.
Usage ¶
- Download the extension and extract it to your extension directory.
- Just follow the following example:
<?php
// Load the extension
Yii::import("ext.EImdbQuery.EImdbQuery");
// Create the object
$movie = new EImdbQuery($model->Title, EImdbQuery::TYPE_TITLE);
// Get data as a property, or
$movie->Title
// Get data as a method
$movie->getTitle();
// Alternatively, to do a search.
$movie = new EImdbQuery($model->Title, EImdbQuery::TYPE_TITLE, false); // set last parameter to false, which defines to do a less strict search
$movie->Greedy = true; // set to true to get all complete results for each movie
// Get results
for($i=0;$i<$movie->getCount();$i++)
{
// Get data as a method
$movie->getTitle($i);
}
// Get data as a property
$movie->Title_0
?>
Available Data ¶
- Title
- Year
- Rated
- Released
- Runtime
- Genre
- Director
- Writer
- Actors
- Plot
- Poster
- imdbRating
- imdbVotes
- imdbID
- Response
Contact ¶
Any bugs or suggestions could be delivered to me through bS.xx7_37@yahoo.com.
hi there, I have made a small fix to your code
Hi there, i added a TYPE_SEARCH so that the user can search easier using your api
<?php
/**
<?php
// Load the extension
Yii::import("ext.EImdbQuery.EImdbQuery");
// Create the object
$movie = new EImdbQuery($model->Title, EImdbQuery::TYPE_TITLE);
// Get data as a property, or
$movie->Title
// Get data as a method
$movie->getTitle();
// Alternatively, to do a search.
$movie = new EImdbQuery($model->Title, EImdbQuery::TYPE_TITLE, false); // set last parameter to false, which defines to do a less strict search
$movie->Greedy = true; // set to true to get all complete results for each movie
// Get results
for($i=0;$i<$movie->getCount();$i++)
{
// Get data as a method $movie->getTitle($i);
}
// Get data as a property
$movie->Title_0
?>
Title
Year
Rated
Released
Runtime
Genre
Director
Writer
Actors
Plot
Poster
imdbRating
imdbVotes
imdbID
Response
*/
class EImdbQuery {
/**
*/
public $DataType = self::DATA_JSON;
/**
*/
public $Plot = self::PLOT_SHORT;
/**
*/
public $ShowNoneAsException = false;
/**
*/
public $NaValue = "Data N/A.";
/**
*/
public $GreedyMode = true;
/**
*/
const PLOT_SHORT = "short";
/**
*/
const PLOT_LONG = "full";
/**
*/
const DATA_JSON = "JSON";
/**
*/
const DATA_XML = "XML";
/**
*/
const TYPE_ID = "i";
/**
*/
const TYPE_TITLE = "t";
/**
*/
const TYPE_SEARCH = "s";
/**
*/
private $_serviceUrl = "http://www.omdbapi.com";
/**
*/
private $_detailUrl = "http://www.imdb.com/title/";
/**
*/
private $_paramYear = "y";
/**
*/
private $_paramDataType = "r";
/**
*/
private $_paramPlot = "plot";
/**
*/
private $_paramSearch = "s";
/**
*/
private $_data = array();
/**
@throws CException
/
public function EImdbQuery($Title = "", $Type = self::TYPE_TITLE, $Year ="", $StrictMode = true) {
if ($Title == "" | is_null($Title)) {
throw new CException("Title can't be empty.");
}
if (!in_array($Type, array(self::TYPE_ID, self::TYPE_TITLE, self::TYPE_SEARCH))) {
throw new CException("Type is invalid, must be i for id, t for title, or s for search");
}
$this->_data = $this->_getMovieDataFromAPI($Title, $Type, $Year, $StrictMode);
if(!$StrictMode)
{
$this->_data = $this->_data["Search"]; if($this->GreedyMode) { for($i=0;$i<count($this->_data);$i++) { $this->_data[$i] = $this->_getMovieDataFromAPI($this->_data[$i]['imdbID'], self::TYPE_ID, "", false); } }
} else
{
$tmp = $this->_data; $this->_data = null; $this->_data[] = $tmp;
}
}
private function _getMovieDataFromAPI($Title, $Type, $Year, $StrictMode)
{
$url = $this->_generateUrl($Title, $Type, $Year, $StrictMode); if(function_exists('curl_init')) { Yii::trace("Using curl to get URL content."); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); }else if(ini_get('allow_url_fopen') != false || ini_get('allow_url_fopen') != "") { Yii::trace("Using fopen to get URL content."); $output = file_get_contents($url); } else { throw new CException("No available method found in server to get URL content. Check server for fopen or curl support."); } $data = null; if($this->DataType == self::DATA_JSON) { Yii::trace("Reading data as JSON."); $JSON = new CJSON(); $data = $JSON->decode($output, true); } else { Yii::trace("Reading data as XML."); $data = reset(get_object_vars(simplexml_load_string($output)->movie)); } return $data;
}
/**
*/
public function __call($name, $arguments) {
if(substr($name, 0, 3) == "get")
{
$propertyname = substr($name, 3); if($this->isMovieFound()) { if(count($arguments>0)) return $this->_getMovieData($propertyname, reset($arguments)); else return $this->_getMovieData($propertyname); } else { return $this->_naAction(); }
} else {
throw new CException("Method $name is invalid.");
}
}
/**
@return mixed
*/
public function __get($name)
{
if($this->isMovieFound())
{
$index = 0; $delimiter = "_"; if(stripos($name, $delimiter) !== false) { $tmp = explode($delimiter, $name); $index = $tmp[1]; $name = $tmp[0]; } return $this->_getMovieData($name, $index);
} else {
return $this->_naAction();
}
}
/**
*/
public function isMovieFound()
{
return ( is_array($this->_data[0]) && !in_array("False",$this->_data[0]) > 0 ) ? true : false;
}
/**
*/
public function getCount()
{
return count($this->_data);
}
/**
*/
private function _naAction()
{
if($this->ShowNoneAsException)
{
throw new CException("Movie was not found.");
} else {
return $this->NaValue;
}
}
/**
*/
public function getDetailUrl($encode = true, $index = 0) {
if($this->isMovieFound())
{
$url = $this->_detailUrl . $this->_data[$index]['imdbID']; if ($encode) { $url = urlencode($url); }
} else {
return $this->NaValue;
}
return $url;
}
/**
*/
private function _getMovieData($propertyname, $index=0)
{
if(array_key_exists($propertyname, $this->_data[$index]) || array_key_exists(strtolower($propertyname), $this->_data[$index]))
{
return isset($this->_data[$index][$propertyname])?$this->_data[$index][$propertyname]:$this->_data[$index][strtolower($propertyname)] ;
} else {
throw new CException("Property $propertyname is invalid.");
}
}
/**
@return string
*/
private function _generateUrl($Title, $Type, $Year = "", $Strict = true) {
$url = $this->_serviceUrl; // http://www.imdbapi.com
$url .= "/?"; // http://www.imdbapi.com/?
if($Strict)
$url .= $Type . "=" . $Title; // http://www.imdbapi.com/?t=Iron+man&y=2010&plot=short&r=JSON
else
{
if($Type == self::TYPE_ID) $url .= $Type . "=" . $Title; else if($Type == self::TYPE_SEARCH) $url .= $this->_paramSearch . "=" . $Title;
}
if (is_numeric($Year)) {
$url .= "&" . $this->_paramYear . "=" . $Year;
}
$url .= "&" . $this->_paramPlot . "=" . $this->Plot;
$url .= "&" . $this->_paramDataType . "=" . $this->DataType;
return str_replace(" ", "+", $url);
}
/**
*/
private function _arrayToObject($array) {
if(!is_array($array)) {
return $array;
}
$object = new stdClass();
if (is_array($array) && count($array) > 0) {
foreach ($array as $name=>$value) {
$name = trim($name); if (!empty($name)) { $object->$name = $this->_arrayToObject($value); }
}
return $object;
}
else {
return FALSE;
}
}
}
?>
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.