Sample Call to Fetch the Catalog in PHP
Since a large number of folks have asked, Here's a sample of how to fetch the Netflix Catalog in PHP
Note that for this example, I'm going to use a open source library I created called OAuthSimple to perform the actual signing process. Unfortunately, there's no standard way (yet) to sign requests, and every library has a different take on how this should be done, mine included. I will attempt to explain things as clearly as possible. I'll also note that this uses PHP5 (although it's not difficult to downgrade it to php4, but really, you shouldn't use php4 anymore).
Please note: The keys and signatures used here are fictional and will not work! Please be sure to substitute in your own key and signature.
Now, let's begin:
Here's the code in one big ol' chunk:
- <?php
- /* The following is a sample code snippet. Please note that the API key and
- shared secret are bogus and will not work.
- I'm using OAuthSimple as the library to perform the signatures. It
- currently is available for PHP and Javascript, but I'd REALLY appreciate
- it if folks could help flesh out versions for Python, .net, and other
- languages.
- */
- include ('./OAuthSimple.php');
- /* Remember, these are bogus. Swap them for the API key and Shared Secret
- you got when you registered your Application at
- http://developer.netflix.com
- */
- $apiKey = 'b0gusK3y001';
- $sharedSecret = '1nv4lidS3krit';
- // Some sample argument values
- /* You can pass in arguments to OAuthSimple either as a string of URL
- characters or an array. (See the documentation for OAuthSimple for
- details. There's nothing magical going on here, just simple key=>value
- pairs. */
- $arguments = Array(
- term=>'fargo',
- expand=>'formats,synopsis',
- max_results=> '1',
- output=>'json'
- );
- // this is the URL path (note the lack of arguments.)
- $path = "http://api.netflix.com/catalog/titles";
- // Create the Signature object.
- $oauth = new OAuthSimple();
- $signed = $oauth->sign(Array(path=>$path,
- parameters=>$arguments,
- signatures=> Array('consumer_key'=>$apiKey,
- 'shared_secret'=>$sharedSecret
- /* If you wanted to do queue functions
- or other things that require access
- tokens and secrets, you'd include them
- here as:
- 'access_token'=>$accessToken,
- 'access_secret'=>$tokenSecret
- */
- )));
- // Now go fetch the data.
- $curl = curl_init();
- curl_setopt($curl,CURLOPT_URL,$signed['signed_url']);
- curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl,CURLOPT_ENCODING,'gzip,deflate');
- curl_setopt($curl,CURLOPT_SETTIMEOUT,2);
- $buffer = curl_exec($curl);
- if (curl_errno($curl))
- {
- die ("An error occurred:".curl_error());
- }
- $result = json_decode($buffer);
- ?>
- <p>
- <b>Your signed URL:</b></br>
- <?php print $signed['signed_url'] ?>;
- </p>
- <p>
- And the output is:</br>
- <pre>
- <?php print print_r($result); ?>
- </pre>
- </p>
And the output is:
- <b>Your signed URL:</b>
- http://api.netflix.com/catalog/titles?expand=formats%2Csynopsis&max_results=1&oauth_consumer_key=b0gusK3y001&oauth_nonce=iYVfb&oauth_signature=2iVzdkZFYIaPWdV9H3kVoeDjrgY%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1228937002&output=json&term=fargo;
- And the output is:
- <pre>
- stdClass Object
- (
- [catalog_titles] => stdClass Object
- (
- [results_per_page] => 1
- [url_template] => http://api.netflix.com/catalog/titles?{-join|&|term|start_index|max_results}
- [link] => Array
- (
- [0] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/index
- [rel] => http://schemas.netflix.com/catalog/titles/index
- [title] => index
- )
- [1] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/autocomplete
- [rel] => http://schemas.netflix.com/catalog/titles/autocomplete
- [title] => autocomplete
- )
- )
- [start_index] => 0
- [number_of_results] => 806
- [catalog_title] => stdClass Object
- (
- [average_rating] => 4.0
- [box_art] => stdClass Object
- (
- [large] => http://cdn-7.nflximg.com/us/boxshots/large/493387.jpg
- [small] => http://cdn-7.nflximg.com/us/boxshots/tiny/493387.jpg
- [medium] => http://cdn-7.nflximg.com/us/boxshots/small/493387.jpg
- )
- [category] => Array
- (
- [0] => stdClass Object
- (
- [term] => R
- [scheme] => http://api.netflix.com/categories/mpaa_ratings
- [label] => R
- )
- [1] => stdClass Object
- (
- [term] => Drama
- [scheme] => http://api.netflix.com/categories/genres
- [label] => Drama
- )
- [2] => stdClass Object
- (
- [term] => Indie Dramas
- [scheme] => http://api.netflix.com/categories/genres
- [label] => Indie Dramas
- )
- [3] => stdClass Object
- (
- [term] => Crime Dramas
- [scheme] => http://api.netflix.com/categories/genres
- [label] => Crime Dramas
- )
- [4] => stdClass Object
- (
- [term] => Dark Humor & Black Comedies
- [scheme] => http://api.netflix.com/categories/genres
- [label] => Dark Humor & Black Comedies
- )
- )
- [id] => http://api.netflix.com/catalog/titles/movies/493387
- [link] => Array
- (
- [0] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/synopsis
- [rel] => http://schemas.netflix.com/catalog/titles/synopsis
- [title] => synopsis
- [synopsis] => Frances McDormand earned an Oscar for her turn as pregnant Sheriff Marge Gunderson, who's sharper than her Northern Minnesota dialect suggests. The intrepid Gunderson bangs on doors and asks questions to unravel a kidnapping plot and the string of murders it provokes. McDormand receives grand support from William H. Macy as a car dealer who conspires with hotheaded kidnappers Steve Buscemi and Peter Stormare.
- )
- [1] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/awards
- [rel] => http://schemas.netflix.com/catalog/titles/awards
- [title] => awards
- )
- [2] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/format_availability
- [rel] => http://schemas.netflix.com/catalog/titles/format_availability
- [title] => formats
- [delivery_formats] => stdClass Object
- (
- [availability] => stdClass Object
- (
- [category] => stdClass Object
- (
- [term] => DVD
- [scheme] => http://api.netflix.com/categories/title_formats
- [label] => DVD
- )
- [available_from] => 966297600
- )
- )
- )
- [3] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/screen_formats
- [rel] => http://schemas.netflix.com/catalog/titles/screen_formats
- [title] => screen formats
- )
- [4] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/cast
- [rel] => http://schemas.netflix.com/catalog/people.cast
- [title] => cast
- )
- [5] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/directors
- [rel] => http://schemas.netflix.com/catalog/people.directors
- [title] => directors
- )
- [6] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/languages_and_audio
- [rel] => http://schemas.netflix.com/catalog/titles/languages_and_audio
- [title] => languages and audio
- )
- [7] => stdClass Object
- (
- [href] => http://api.netflix.com/catalog/titles/movies/493387/similars
- [rel] => http://schemas.netflix.com/catalog/titles.similars
- [title] => similars
- )
- [8] => stdClass Object
- (
- [href] => http://www.netflix.com/Movie/Fargo/493387
- [rel] => alternate
- [title] => web page
- )
- )
- [runtime] => 5880
- [title] => stdClass Object
- (
- [regular] => Fargo
- [short] => Fargo
- )
- [release_year] => 1996
- )
- )
- )
- 1
- </pre>
Notice that values in the URL are encoded (e.g. the oauth_signature=2iVzdkZFYIaPWdV9H3kVoeDjrgY%3D) Also notice that the secrets are not included in the URL, they're only used for signing the request.
Needless to say, I'm very interested in any feedback you may have and will happily offer additional examples once someone wants to provide them.
