2

Hello stackoverflow users. I need to pull some movie information from the Open Movie Database API. They have no docs on how to use their API so i am very confused. The site i am creating in PHP needs to pull some variables or strings from their api depending on what imdb ID i put in an form. Here is the code i got so far. This gives me: "http://www.omdbapi.com/?i=tt1092026". But i need to get the strings of their API and make them to variables so i can use them in forms later. How do i do this? Please help. Thanks! :D

<form action="?action=grab" method="post">
<input placeholder="tt1092026" type="text" name="id" id="id">
<input type="submit" class="button" value="Grab Movie">
</form>
<?php if ($_GET[action] == "grab") { ?>
<h9>
<?php
$id = $_POST["id"];
$url = "http://www.omdbapi.com/?i=$id";
echo $url;

?>
</h9>
<?php }; ?>
Elias Nilsson
  • 21
  • 1
  • 2
  • possible duplicate of [Get file content from a URL?](http://stackoverflow.com/questions/5522636/get-file-content-from-a-url) – 000 Mar 31 '13 at 17:15

2 Answers2

2

You need to decode the response and assign it to variables (all in PHP).

<?php
$id = $_POST["id"];
$url = file_get_contents("http://www.omdbapi.com/?i=$id");
$json = json_decode($url, true); //This will convert it to an array
$movie_title = $json['Title'];
$movie_year = $json['Year'];
//and so on...

and then when you need to echo them:

echo $movie_title;

or

echo "The movie '$movie_title' was made in $movie_year.";
domo270897
  • 58
  • 7
0

I think TMDB is is a better API. http://www.themoviedb.org They have very good documentation here: http://docs.themoviedb.apiary.io

I'm using this in my own web application and it works like a charm.

TimDMeyer
  • 77
  • 5