1

I found this site, that provides IMDB API: http://www.omdbapi.com

and for getting for example the hobbit's it's easy enough as this: http://www.omdbapi.com/?i=tt0903624

Then I get all this information:

{"Title":"The Hobbit: An Unexpected Journey","Year":"2012","Rated":"11","Released":"14 Dec 2012","Runtime":"2 h 46 min","Genre":"Adventure, Fantasy","Director":"Peter Jackson","Writer":"Fran Walsh, Philippa Boyens","Actors":"Martin Freeman, Ian McKellen, Richard Armitage, Andy Serkis","Plot":"A curious Hobbit, Bilbo Baggins, journeys to the Lonely Mountain with a vigorous group of Dwarves to reclaim a treasure stolen from them by the dragon Smaug.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTkzMTUwMDAyMl5BMl5BanBnXkFtZTcwMDIwMTQ1OA@@._V1_SX300.jpg","imdbRating":"9.2","imdbVotes":"5,666","imdbID":"tt0903624","Response":"True"}

The thing is that I only want for exmaple the title, the year and the plot information, and I wonder how I can only retrieve this.

I want to use PHP.

2 Answers 2

5

Here you go... simply decode the json, and pull out the data you need. If need be, you can re-encode it as json afterwards.

$data = file_get_contents('http://www.omdbapi.com/?i=tt0903624');
$data = json_decode($data, true);
$data = array('Title' => $data['Title'], 'Plot' => $data['Plot']);
$data = json_encode($data);
print($data);

Another way to do this (slightly more efficiently) is to unset unneeded keys, e.g.:

$data = file_get_contents('http://www.omdbapi.com/?i=tt0903624');
$data = json_decode($data, true);
$keys = array_keys($data);
foreach ($keys as $key) {
    if ($key != 'Title' && $key != 'Plot) {
        unset($data[$key]);
    }
}
$data = json_encode($data);
print($data);
Sign up to request clarification or add additional context in comments.

2 Comments

Got this when i tried the top solution:"Fatal error: Cannot use object of type stdClass as array in"
That's because the default for json_decode is as an object. I've added an edit (whenever it gets reviewed) which fixes that by setting the second parameter to decode as an array. As an alternative, you can access the default json_decode data using object notation in the first example like so: 'Title' => $data->Title, 'Plot' => $data->Plot
0

OMDBAPI.com is no longer free to use. As you can read on their website:

05/08/17 - Going Private! Please go read the post on the Patreon page about this major change. 

This means you must become a donator to get access to the API. I used their API for over a year and now it stopped. If you need to perform lots of queries then I think becoming a sponsor to OMDBAPI is a good idea. However I used their API in my little private project. After googling a bit I found another API. Here's the code you can use:

<?php
$imdbID = 'tt2866360';
$data = json_decode(file_get_contents('http://api.rest7.com/v1/movie_info.php?imdb=' . $imdbID));

if (@$data->success !== 1)
{
    die('Failed');
}
echo '<pre>';
print_r($data->movies[0]);

I am not affiliated with this website. But I use this API so can answer a question or two if anyone has.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.