Third-Party Web APIs

  1. Overview
    1. A Web Application Programming Interface or Web API is a service that a client program can interface with using HTTP
    2. A third-party web API is a service provided by a third party, which is often free but requires registering for a key to access it
    3. Many third-party web APIs provides access to a large data set
    4. Some popular web APIs:
      1. Flickr API (photos)
      2. Bing Search API (web search)
      3. Facebook Graph API (Facebook social graph)
      4. Yahoo Fantasy Sports API
    5. Most third-party web APIs are RESTful, meaning they use a query string or path or access the API and return the results in XML or JSON
    6. Some web APIs are SOAP-based, which is more complicated and heavily dependent on transfering XML between the client and server
  2. General architecture
    1. Client-side usage (with CORS, JSONP, or proxy server)
                 API Req
      1) Browser ---------> Service Provider 
      
                 XML/JSON
      2) Browser <--------- Service Provider           
      
    2. Server-side usage
                 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 
      
  3. OMDb API
    1. OMDb API is a free web API to obtain movie information
    2. The movie data is obtained from IMDb
    3. You must obtain a free API key to use the service
    4. Example queries
      1. Example 1 - Search for all movies with the word "star" in the title

        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"
            }
          ]
        }
        
      2. Example 2 - Get info for movie with IMDB ID tt0076759 (returns JSON)

        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"
        }
        
  4. Accessing the movie API in JavaScript
    1. Use XMLHttpRequest to make GET request
    2. Set responseType to "json" if results are JSON-encoded
    3. Use encodeURIComponent() to ensure string used in HTTP request is URL-encoded
    4. Example
      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);
      		}
      	}
      }
      
  5. Accessing a movie API in PHP
    1. Use file_get_contents($url) to make an HTTP request to the web API and get back the HTTP response body
      $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>";
      }
      
    2. Use json_decode($json) to parse the JSON and get back an object with a 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...
              )
      )
      
    3. Example outputting the first movie title
      echo "First title = " . $obj->Search[0]->Title;
      
      Output:
      First title = Star Wars: Episode IV - A New Hope
      
    4. Example displaying the title of each movie
      // 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";
      }