Created
October 2, 2009 22:51
-
-
Save fangel/200210 to your computer and use it in GitHub Desktop.
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 | |
require 'OAuth.php'; | |
$consumer = new OAuthConsumer('consumer-key', 'consumer-secret'); | |
$token = new OAuthToken('token-key', 'token-secret'); // can be both request and access-token | |
// of course it needs to be an access-token to perform authorized request, but for the call to | |
// exchange your request-token for an access-token, this needs to be the request-token | |
$params = array( | |
'here' => 'is list of your non oauth_*-parameters', | |
'in' => 'a nice an convenient list' | |
); | |
$signature_method = new OAuthSignatureMethod_HMAC_SHA1(); // Or whatever SignatureMethod you want to use | |
$req = OAuthRequest::from_consumer_and_token( | |
$consumer, | |
$token, | |
'GET', // Or POST if you need to do POST requests | |
'http://service-provider.example.com/end_point', // Your URL | |
$params | |
); | |
$req->sign_request($signature_method, $consumer, $token); | |
// By now the request is signed and contained in the OAuthRequest.. | |
// So now we just need to send it along - this is an example using cURL | |
$curl = curl_init(); | |
$_params = $req->get_parameters(); | |
foreach( array_keys($_params) AS $i ) | |
if( substr($i, 0, 6) == 'oauth_' ) | |
unset($_params[$i]); | |
// Now $_params contains a list of your parameters without the oauth_*-parameters which we | |
// wil send in the header | |
$url = $req->get_normalized_http_url(); | |
if( $req->get_normalized_http_method() == 'POST' ) { | |
curl_setopt($curl, CURLOPT_POST, true); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_params) ); | |
} else { | |
if( count($params) ) | |
$url .= '?' . http_build_query($_params); | |
} | |
// And we added the non oauth_* parameters to the request | |
curl_setopt($curl, CURLOPT_URL, $url); | |
// Set the URL | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
// We like returntransfer ;) | |
curl_setopt($curl, CURLOPT_HTTPHEADER, array( | |
$req->to_header() | |
)); | |
// Set the Authorization-header containing the oauth_* parameters | |
$respone = curl_exec($curl); | |
// There, request performed - response in $response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment