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:

  1. <?php  
  2.     /* The following is a sample code snippet. Please note that the API key and 
  3.       shared secret are bogus and will not work. 
  4.        
  5.       I'm using OAuthSimple as the library to perform the signatures. It 
  6.       currently is available for PHP and Javascript, but I'd REALLY appreciate 
  7.       it if folks could help flesh out versions for Python, .net, and other 
  8.       languages. 
  9.     */  
  10.       
  11.     include ('./OAuthSimple.php');  
  12.           
  13.     /* Remember, these are bogus. Swap them for the API key and Shared Secret 
  14.       you got when you registered your Application at 
  15.       http://developer.netflix.com 
  16.     */  
  17.     $apiKey = 'b0gusK3y001';  
  18.     $sharedSecret = '1nv4lidS3krit';  
  19.       
  20.     // Some sample argument values  
  21.   
  22.     /* You can pass in arguments to OAuthSimple either as a string of URL 
  23.       characters or an array. (See the documentation for OAuthSimple for 
  24.       details. There's nothing magical going on here, just simple key=>value 
  25.       pairs. */  
  26.     $arguments = Array(  
  27.         term=>'fargo',  
  28.         expand=>'formats,synopsis',  
  29.         max_results=> '1',  
  30.         output=>'json'  
  31.     );  
  32.       
  33.     // this is the URL path (note the lack of arguments.)  
  34.     $path = "http://api.netflix.com/catalog/titles";  
  35.   
  36.     // Create the Signature object.  
  37.     $oauth = new OAuthSimple();  
  38.     $signed = $oauth->sign(Array(path=>$path,  
  39.                     parameters=>$arguments,  
  40.                     signatures=> Array('consumer_key'=>$apiKey,  
  41.                                         'shared_secret'=>$sharedSecret  
  42.                                         /* If you wanted to do queue functions 
  43.                                           or other things that require access 
  44.                                           tokens and secrets, you'd include them 
  45.                                           here as: 
  46.                                         'access_token'=>$accessToken, 
  47.                                         'access_secret'=>$tokenSecret 
  48.                                         */  
  49.                                         )));  
  50.       
  51.     // Now go fetch the data.  
  52.     $curl = curl_init();  
  53.     curl_setopt($curl,CURLOPT_URL,$signed['signed_url']);  
  54.     curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);  
  55.     curl_setopt($curl,CURLOPT_ENCODING,'gzip,deflate');
  56.     curl_setopt($curl,CURLOPT_SETTIMEOUT,2);  
  57.     $buffer = curl_exec($curl);  
  58.     if (curl_errno($curl))  
  59.     {  
  60.         die ("An error occurred:".curl_error());  
  61.     }  
  62.     $result = json_decode($buffer);  
  63. ?>  
  64. <p>  
  65. <b>Your signed URL:</b></br>  
  66. <?php print $signed['signed_url'] ?>;  
  67. </p>  
  68. <p>  
  69. And the output is:</br>  
  70. <pre>  
  71. <?php print print_r($result); ?>  
  72. </pre>  
  73. </p>  

And the output is:

  1. <b>Your signed URL:</b>  
  2. 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;  
  3.   
  4. And the output is:  
  5. <pre>  
  6. stdClass Object  
  7. (  
  8.     [catalog_titles] => stdClass Object  
  9.         (  
  10.             [results_per_page] => 1  
  11.             [url_template] => http://api.netflix.com/catalog/titles?{-join|&|term|start_index|max_results}  
  12.             [link] => Array  
  13.                 (  
  14.                     [0] => stdClass Object  
  15.                         (  
  16.                             [href] => http://api.netflix.com/catalog/titles/index  
  17.                             [rel] => http://schemas.netflix.com/catalog/titles/index  
  18.                             [title] => index  
  19.                         )  
  20.   
  21.                     [1] => stdClass Object  
  22.                         (  
  23.                             [href] => http://api.netflix.com/catalog/titles/autocomplete  
  24.                             [rel] => http://schemas.netflix.com/catalog/titles/autocomplete  
  25.                             [title] => autocomplete  
  26.                         )  
  27.   
  28.                 )  
  29.   
  30.             [start_index] => 0  
  31.             [number_of_results] => 806  
  32.             [catalog_title] => stdClass Object  
  33.                 (  
  34.                     [average_rating] => 4.0  
  35.                     [box_art] => stdClass Object  
  36.                         (  
  37.                             [large] => http://cdn-7.nflximg.com/us/boxshots/large/493387.jpg  
  38.                             [small] => http://cdn-7.nflximg.com/us/boxshots/tiny/493387.jpg  
  39.                             [medium] => http://cdn-7.nflximg.com/us/boxshots/small/493387.jpg  
  40.                         )  
  41.   
  42.                     [category] => Array  
  43.                         (  
  44.                             [0] => stdClass Object  
  45.                                 (  
  46.                                     [term] => R  
  47.                                     [scheme] => http://api.netflix.com/categories/mpaa_ratings  
  48.                                     [label] => R  
  49.                                 )  
  50.   
  51.                             [1] => stdClass Object  
  52.                                 (  
  53.                                     [term] => Drama  
  54.                                     [scheme] => http://api.netflix.com/categories/genres  
  55.                                     [label] => Drama  
  56.                                 )  
  57.   
  58.                             [2] => stdClass Object  
  59.                                 (  
  60.                                     [term] => Indie Dramas  
  61.                                     [scheme] => http://api.netflix.com/categories/genres  
  62.                                     [label] => Indie Dramas  
  63.                                 )  
  64.   
  65.                             [3] => stdClass Object  
  66.                                 (  
  67.                                     [term] => Crime Dramas  
  68.                                     [scheme] => http://api.netflix.com/categories/genres  
  69.                                     [label] => Crime Dramas  
  70.                                 )  
  71.   
  72.                             [4] => stdClass Object  
  73.                                 (  
  74.                                     [term] => Dark Humor & Black Comedies  
  75.                                     [scheme] => http://api.netflix.com/categories/genres  
  76.                                     [label] => Dark Humor & Black Comedies  
  77.                                 )  
  78.   
  79.                         )  
  80.   
  81.                     [id] => http://api.netflix.com/catalog/titles/movies/493387  
  82.                     [link] => Array  
  83.                         (  
  84.                             [0] => stdClass Object  
  85.                                 (  
  86.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/synopsis  
  87.                                     [rel] => http://schemas.netflix.com/catalog/titles/synopsis  
  88.                                     [title] => synopsis  
  89.                                     [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.  
  90.                                 )  
  91.   
  92.                             [1] => stdClass Object  
  93.                                 (  
  94.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/awards  
  95.                                     [rel] => http://schemas.netflix.com/catalog/titles/awards  
  96.                                     [title] => awards  
  97.                                 )  
  98.   
  99.                             [2] => stdClass Object  
  100.                                 (  
  101.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/format_availability  
  102.                                     [rel] => http://schemas.netflix.com/catalog/titles/format_availability  
  103.                                     [title] => formats  
  104.                                     [delivery_formats] => stdClass Object  
  105.                                         (  
  106.                                             [availability] => stdClass Object  
  107.                                                 (  
  108.                                                     [category] => stdClass Object  
  109.                                                         (  
  110.                                                             [term] => DVD  
  111.                                                             [scheme] => http://api.netflix.com/categories/title_formats  
  112.                                                             [label] => DVD  
  113.                                                         )  
  114.   
  115.                                                     [available_from] => 966297600  
  116.                                                 )  
  117.   
  118.                                         )  
  119.   
  120.                                 )  
  121.   
  122.                             [3] => stdClass Object  
  123.                                 (  
  124.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/screen_formats  
  125.                                     [rel] => http://schemas.netflix.com/catalog/titles/screen_formats  
  126.                                     [title] => screen formats  
  127.                                 )  
  128.   
  129.                             [4] => stdClass Object  
  130.                                 (  
  131.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/cast  
  132.                                     [rel] => http://schemas.netflix.com/catalog/people.cast  
  133.                                     [title] => cast  
  134.                                 )  
  135.   
  136.                             [5] => stdClass Object  
  137.                                 (  
  138.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/directors  
  139.                                     [rel] => http://schemas.netflix.com/catalog/people.directors  
  140.                                     [title] => directors  
  141.                                 )  
  142.   
  143.                             [6] => stdClass Object  
  144.                                 (  
  145.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/languages_and_audio  
  146.                                     [rel] => http://schemas.netflix.com/catalog/titles/languages_and_audio  
  147.                                     [title] => languages and audio  
  148.                                 )  
  149.   
  150.                             [7] => stdClass Object  
  151.                                 (  
  152.                                     [href] => http://api.netflix.com/catalog/titles/movies/493387/similars  
  153.                                     [rel] => http://schemas.netflix.com/catalog/titles.similars  
  154.                                     [title] => similars  
  155.                                 )  
  156.   
  157.                             [8] => stdClass Object  
  158.                                 (  
  159.                                     [href] => http://www.netflix.com/Movie/Fargo/493387  
  160.                                     [rel] => alternate  
  161.                                     [title] => web page  
  162.                                 )  
  163.   
  164.                         )  
  165.   
  166.                     [runtime] => 5880  
  167.                     [title] => stdClass Object  
  168.                         (  
  169.                             [regular] => Fargo  
  170.                             [short] => Fargo  
  171.                         )  
  172.   
  173.                     [release_year] => 1996  
  174.                 )  
  175.   
  176.         )  
  177.   
  178. )  
  179. 1  
  180. </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.

54XqbMADta