Skip to page content

Working with the Netflix API in PHP

Introduction

I spent part of last week working on an entry for the Boxee/Twilio developer contest. A few days before the deadline, I decided it’d be really slick if I added Netflix into the mix, so I started digging through the documentation. I think Twilio’s simple API and awesome debugging tools spoiled me because figuring out Netflix’s API was a pain. A lot of this had to do with there not being a really good PHP library to take care of authorization and making signed calls to the API. I went with OAuthSimple and ran into a lot of “invalid signature” errors and other little gotchas along the way. Hopefully, this write-up and sample code will save you guys some time.

Before You Begin

In order to get started, you’ll need to sign up for a Netflix developer account and apply for an API key. (This is separate from your normal Netflix account.) After you’ve been approved (as far as I can tell, approval is instant), you’ll receive a “key” and a “shared secret.” From here on out, we’ll call them the “consumer key” and “consumer secret.”

Screenshot of the Netflix API key management page.

Write both of those down, because we’ll be using them often. You’ll also want to get yourself a copy of the OAuthSimple library.

Getting Authorized

Netflix uses OAuth, so you’ll need to have users authorize your application before you can work with their data. In order to do this, we’ll create a link that takes them to the authorization page.

If all goes well and the user authorizes the application, Netflix will call the page you specified in the link above and pass it the user’s OAuth token. You can then exchange that temporary token for a permanent OAuth token, OAuth token secret, and user ID. You’ll want to save this information in your database (or however you’re storing data) since you’ll be using it to make calls to the Netflix API.

Making Calls to the API

Now that the user has authorized your application, you’re ready to actually start working with the API [1]. The API is REST based, so it’s as simple as specifying a URL, passing in the proper parameters, and parsing the data returned.

For example, here’s how to get a list of the movies in the user’s Instant Watch queue:

Adding movies to a user’s queue is similar—just specify the URL (http://api.netflix.com/users/[netflix_user_id]/queues/instant/disc), pass in the proper parameters, and parse the data returned. Also, remember that when writing data, set cURL to POST the data (curl_setopt($curl, CURLOPT_POST, true) and curl_setopt($curl, CURLOPT_POSTFIELDS, [parameters to pass])) and set the action to POST in the OAuthSimple library ($oauth->setAction('POST')).

Check out Netflix’s documentation for other examples of common tasks.

Notes

  1. You can work with some parts of the Netflix API without being authorized by the user, but nothing too interesting—just searching the Netflix catalog for movies/TV shows.