Recommended:

  • phpclasses.org
  • jsclasses.org
  • jsmag.com
  • siteapps.com
  • View our reviews on Hot Scripts
  • JS Tutorial
  • scripts.com
  • securesignup.com




Recent Comments

Powered by Disqus




Back to articles

Get list of episodes from IMDB

Here is the piece of code that will get a list of episodes for TV series from this unofficial IMDB API. This function takes TV series name as a parameter and you may also specify year, if there are more TV series with same name. It returns an array with all episodes of specified TV series structured by seasons:

<?php
//name - title of TV series
//year - year of TV series if there are multiple with same title
function get_episodes($name, $year = "")
{
    //url to connect to
    $url = "http://imdbapi.poromenos.org/js/?name=".urlencode($name); 
    if(trim($year) == "")
    {
        $url.="&year=".urlencode($year);
    }
    // send request 
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $curlData = curl_exec($curl);
    curl_close($curl);
    //decode json data
    $data = json_decode($curlData, true);
    $arr = array();
    //restructuring data to by seasons and episodes
    foreach($data as $key => $val)
    {
        if(is_array($val))
        {
            foreach($val as $key => $value)
            {
                if($key == "episodes")
                {
                    foreach($value as $num)
                    {
                        $arr[$num['season']][$num['number']] = $num['name'];
                    }
                }
            }
        }
        else
        {
            echo "<li>".$key.": ".$val."</li>";
        }
    }
    //sorting episodes
    foreach($arr as $key => $season)
    {
        ksort($arr[$key]);
    }
    //sorting seasons and returning array
    ksort($arr);
    return $arr;
}

$episodes = get_episodes("How I met your mother");
echo "<pre>";
print_r($episodes);
echo "</pre>";
?>

You may also be interested in:

Powered by BlogAlike.com

blog comments powered by Disqus