API Req 1) Browser ---------> Service Provider XML/JSON 2) Browser <--------- Service Provider
Request 1) Browser ---------> Web Server API Req 2) Browser Web Server ---------> Service Provider XML/JSON 3) Browser Web Server <--------- Service Provider Response 4) Browser <--------- Web Server Service Provider
http://www.omdbapi.com/?s=star&type=movie&apikey=APIKEY
{ "Search": [ { "Title": "Star Wars: Episode IV - A New Hope", "Year": "1977", "imdbID": "tt0076759", "Type": "movie" }, { "Title": "Star Wars: Episode V - The Empire Strikes Back", "Year": "1980", "imdbID": "tt0080684", "Type": "movie" }, etc... { "Title": "Star Trek II: The Wrath of Khan", "Year": "1982", "imdbID": "tt0084726", "Type": "movie" } ] }
http://www.omdbapi.com/?i=tt0076759&apikey=APIKEY
{ "Title":"Star Wars: Episode IV - A New Hope", "Year":"1977", "Rated":"PG", "Released":"25 May 1977", "Runtime":"121 min", "Genre":"Action, Adventure, Fantasy", "Director":"George Lucas", "Writer":"George Lucas", "Actors":"Mark Hamill, Harrison Ford, Carrie Fisher, Peter Cushing", "Plot":"Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a Wookiee, and two droids to save the galaxy from the Empire's world-destroying battle-station, while also attempting to rescue Princess Leia from the evil Darth Vader.", "Language":"English", "Country":"USA", "Awards":"Won 6 Oscars. Another 50 wins & 28 nominations.", "Poster":"https://images-na.ssl-images-amazon.com/images/M/MV5BZDk2NmNhZDgtZDgzZS00NTRkLWFiYjUtNGMzZTYwNTFhYjFmXkEyXkFqcGdeQXVyNzkwMjQ5NzM@._V1_SX300.jpg", "Ratings":[ { "Source":"Internet Movie Database", "Value":"8.7/10" }, { "Source":"Rotten Tomatoes", "Value":"93%" }, { "Source":"Metacritic", "Value":"92/100" } ], "Metascore":"92", "imdbRating":"8.7", "imdbVotes":"1,009,663", "imdbID":"tt0076759", "Type":"movie", "DVD":"21 Sep 2004", "BoxOffice":"N/A", "Production":"20th Century Fox", "Website":"http://www.starwars.com/episode-iv/", "Response":"True" }
XMLHttpRequest
to make GET request
responseType
to "json" if results are JSON-encoded
encodeURIComponent()
to ensure string used
in HTTP request is URL-encoded
var search = "star wars";
var xhr = new XMLHttpRequest();
xhr.addEventListener("load", responseReceived);
xhr.responseType = "json";
xhr.open("GET", "https://www.omdbapi.com/?s=" + encodeURIComponent(search) + "&type=movie&apikey=APIKEY");
xhr.send();
function responseReceived() {
// Display all movie titles
if (this.response.Error) {
console.log("Movie not found");
}
else {
var movies = this.response.Search;
for (var i = 0; i < movies.length; i++) {
console.log(movies[i].Title);
}
}
}
$search = urlencode("star wars"); // URL-encode the string
$apikey = "APIKEY";
// Make HTTP request and wait for the response
$response = file_get_contents("http://www.omdbapi.com/?s=$search&y=&plot=short&r=json&apikey=$apikey");
if ($response === FALSE)
die("Error contacting the web API");
else
{
// Display JSON results
echo "<pre>$response</pre>";
}
Search
property that is an array of objects containing the movie data
// Parse JSON results
$obj = json_decode($response);
print_r($obj);
Output:
( [Search] => Array ( [0] => stdClass Object ( [Title] => Star Wars: Episode IV - A New Hope [Year] => 1977 [imdbID] => tt0076759 [Type] => movie ) [1] => stdClass Object ( [Title] => Star Wars: Episode V - The Empire Strikes Back [Year] => 1980 [imdbID] => tt0080684 [Type] => movie ) etc... ) )
echo "First title = " . $obj->Search[0]->Title;
Output:
First title = Star Wars: Episode IV - A New Hope
// Get the array of objects containing movie data
$search_results = $obj->Search;
// Loop through every movie
foreach ($search_results as $movie)
{
echo "<p>Title: $movie->Title</p>\n";
}