Skip to content

Instantly share code, notes, and snippets.

@rahims
Created April 14, 2010 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahims/366207 to your computer and use it in GitHub Desktop.
Save rahims/366207 to your computer and use it in GitHub Desktop.
Example PHP code for working with the Netflix API using the OAuthSimple library.
<?php
// OAuthSimple can be found at http://github.com/jrconlin/oauthsimple/
require_once('OAuthSimple.php');
// You'll likely want to move $consumer_key and $consumer_secret to an
// include file
$consumer_key = '[The key given to you by Netflix]';
$consumer_secret = '[The shared secret given to you by Netflix]';
$oauth_token = $_GET['oauth_token'];
$oauth_secret = file_get_contents($oauth_token.'.tmp');
$access_token_url = 'http://api-public.netflix.com/oauth/access_token';
$params = array(
'oauth_token' => $oauth_token
);
$signatures = array(
'consumer_key' => $consumer_key,
'shared_secret' => $consumer_secret,
'oauth_secret' => $oauth_secret
);
$oauth = new OAuthSimple();
$signed = $oauth->sign(Array(
'path' => $access_token_url,
'parameters' => $params,
'signatures' => $signatures
));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $signed['signed_url']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$buffer = curl_exec($curl);
curl_close($curl);
preg_match('/oauth_token=(.*?)&/i', $buffer, $tokenMatches);
preg_match('/oauth_token_secret=(.*?)$/i', $buffer, $secretMatches);
preg_match('/user_id=(.*?)&/i', $buffer, $userIdMatches);
echo 'Successfully linked your Netflix account!';
// Save $tokenMatches[1], $secretMatches[1], and $userIdMatches[1] to your
// database for later use.
echo $tokenMatches[1]."\n";
echo $secretMatches[1]."\n";
echo $userIdMatches[1]."\n";
?>
<?php
// OAuthSimple can be found at http://github.com/jrconlin/oauthsimple/
require_once('OAuthSimple.php');
// You'll likely want to move $consumer_key and $consumer_secret to an
// include file
$consumer_key = '[The key given to you by Netflix]';
$consumer_secret = '[The shared secret given to you by Netflix]';
$oauth_token = "[user's OAuth token value from the database]";
$oauth_token_secret = "[user's OAuth token secret value from the database]";
$userId = "[user's Netflix user ID from the database]";
$get_queue_url = 'http://api-public.netflix.com/users/'.$userId.'/queues/instant/available';
$params = array(
'oauth_token' => $oauth_token,
'output' => 'json'
);
$signatures = array(
'consumer_key' => $consumer_key,
'shared_secret' => $consumer_secret,
'oauth_secret' => $oauth_token_secret
);
$oauth = new OAuthSimple();
$signed = $oauth->sign(Array(
'path' => $get_queue_url,
'parameters' => $params,
'signatures' => $signatures
));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $signed['signed_url']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$buffer = curl_exec($curl);
curl_close($curl);
$jsonResult = json_decode($buffer, true);
// Do whatever you want with the result. In this case, we'll just dump it
// to the browser.
print_r($jsonResult);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment