0

i have created a search option for a site to get movie details using mysql database BUT i want to connect them using the omdb api but i cant figure out please help me with this

this is my db connection using mysql database

<?php

//variables
$server = "localhost";
$username = "root";
$password = "";
$dbname = "devsearch";


$conn = mysqli_connect($server, $username, $password, $dbname);

OMDb API: http://www.omdbapi.com/?i=tt3896198&apikey=[MY_API_KEY]

Stephane L
  • 2,879
  • 1
  • 34
  • 44
NAND
  • 130
  • 9
  • Do you mean you want to store movies from the API into your database, or just search it? – atymic Jul 20 '19 at 09:36
  • just search it and display eg: title year and etc – NAND Jul 20 '19 at 09:39
  • I don't quite understand your question. Could you tell us what information is stored in your MySQL database? So, when you search it, with the search option you already have, what is returned? An IMDb ID? You want to use that to retrieve information from the Open Movie Database? Their API returns JSON or XML. Is your question about how to retrieve this JSON or XML with a PHP script? – KIKO Software Jul 20 '19 at 09:54
  • i want to retireve information from the open movie database? – NAND Jul 20 '19 at 10:04

2 Answers2

1

If your question is about how to retrieve information from the Open Movie Database, with a PHP script, you can use this PHP code:

function getImdbRecord($ImdbId, $ApiKey)
{
    $path = "http://www.omdbapi.com/?i=$ImdbId&apikey=$ApiKey";
    $json = file_get_contents($path);
    return json_decode($json, TRUE);
}

$data = getImdbRecord("tt3896198", "f3d054e8");

echo "<pre>";
print_r($data);
echo "</pre>";

Please read the tip in the PHP manual about fopen wrappers if you have trouble reading from an URL.

The code above will return this result:

Array
(
    [Title] => Guardians of the Galaxy Vol. 2
    [Year] => 2017
    [Rated] => PG-13
    [Released] => 05 May 2017
    [Runtime] => 136 min
    [Genre] => Action, Adventure, Comedy, Sci-Fi
    [Director] => James Gunn
    [Writer] => James Gunn, Dan Abnett (based on the Marvel comics by), Andy Lanning (based on the Marvel comics by), Steve Englehart (Star-Lord created by), Steve Gan (Star-Lord created by), Jim Starlin (Gamora and Drax created by), Stan Lee (Groot created by), Larry Lieber (Groot created by), Jack Kirby (Groot created by), Bill Mantlo (Rocket Raccoon created by), Keith Giffen (Rocket Raccoon created by), Steve Gerber (Howard the Duck created by), Val Mayerik (Howard the Duck created by)
    [Actors] => Chris Pratt, Zoe Saldana, Dave Bautista, Vin Diesel
    [Plot] => The Guardians struggle to keep together as a team while dealing with their personal family issues, notably Star-Lord's encounter with his father the ambitious celestial being Ego.
    [Language] => English
    [Country] => USA
    [Awards] => Nominated for 1 Oscar. Another 12 wins & 42 nominations.
    [Poster] => https://m.media-amazon.com/images/M/MV5BMTg2MzI1MTg3OF5BMl5BanBnXkFtZTgwNTU3NDA2MTI@._V1_SX300.jpg
    [Ratings] => Array
        (
            [0] => Array
                (
                    [Source] => Internet Movie Database
                    [Value] => 7.7/10
                )

            [1] => Array
                (
                    [Source] => Rotten Tomatoes
                    [Value] => 84%
                )

            [2] => Array
                (
                    [Source] => Metacritic
                    [Value] => 67/100
                )

        )

    [Metascore] => 67
    [imdbRating] => 7.7
    [imdbVotes] => 489,848
    [imdbID] => tt3896198
    [Type] => movie
    [DVD] => 22 Aug 2017
    [BoxOffice] => $389,804,217
    [Production] => Walt Disney Pictures
    [Website] => https://marvel.com/guardians
    [Response] => True
)
KIKO Software
  • 15,283
  • 3
  • 18
  • 33
0

You are accessing the Json API. you can extract the JSON data using

json_decode()

this link will be helpfull to you How do I extract data from JSON with PHP?

Dhairya Lakhera
  • 4,445
  • 3
  • 35
  • 62