Created
April 14, 2010 19:25
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]'; | |
$callback_url = '[The URL of your callback page]'; | |
$request_token_url = 'http://api-public.netflix.com/oauth/request_token'; | |
$signatures = array( | |
'consumer_key' => $consumer_key, | |
'shared_secret' => $consumer_secret | |
); | |
$oauth = new OAuthSimple(); | |
$signed = $oauth->sign(Array( | |
'path' => $request_token_url, | |
'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); | |
// We need the value of oauth_token_secret in the callback page, so we'll save | |
// it to a file temporarily. | |
file_put_contents($tokenMatches[1].'.tmp', $secretMatches[1]); | |
echo '<a href="https://api-user.netflix.com/oauth/login?oauth_callback='.$callback_url.'&oauth_consumer_key='.$consumer_key.'&oauth_token='.$tokenMatches[1].'">Link your Netflix account.</a>'; | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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