How can i parse (take) the imdb score of a movie.

Discussion in 'PHP' started by Argento, Mar 25, 2009.

  1. #1
    Hi, i need to take the imdb score of a movie and save it into a table.

    But i dont have idea how can i take the score.

    For example for this url:

    http://www.imdb.com/title/tt0167260/

    The score is 8.8/10

    So i will aprecciate any help, thanks alot.
     
    Argento, Mar 25, 2009 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Read the page into a variable and use preg_match to parse the score.

    Peace,
     
    Barti1987, Mar 25, 2009 IP
  3. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #3
    function get_imdb_score($url)
    {
       if (!($file = file_get_contents($url)))
          trigger_error('Unable to retrieve page from IMDB', E_USER_ERROR);
       if (!preg_match('/class="meta">\s*<b>([\d.]+)\/10/', $file, $matches))
          trigger_error('Unable to parse IMDB response', E_USER_ERROR);
       return $matches[1];
    }
    
    echo 'Score = ' . get_imdb_score('http://www.imdb.com/title/tt0167260/');
    Code (markup):
    Obviously this may stop working whenever IMDB tinkers with their page layout. They may also get bored of you querying them too many times and block you from connecting, so be sure to cache results in your own local database rather than hitting them every time you want to know the same movie's score.
     
    SmallPotatoes, Mar 26, 2009 IP
    Barti1987 likes this.
  4. Argento

    Argento Active Member

    Messages:
    69
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    53
    #4
    Thanks alot SmallPotatoes !!! i will save the results in the database, thanks !
     
    Argento, Mar 26, 2009 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    PoPSiCLe, Mar 27, 2009 IP