Skip to content

Instantly share code, notes, and snippets.

@fangel
Created June 14, 2009 12:49
Show Gist options
  • Save fangel/129669 to your computer and use it in GitHub Desktop.
Save fangel/129669 to your computer and use it in GitHub Desktop.
<?php
/**
* A simple sample OAuth client implementation
*
* :NOTE: This is not Revision A. compliant. I haven't had the time to update it.
*
* :NOTE 2: This is adapted from a more specific-case client, and I haven't tested
* the generalization!
*
* @requires OAuth-php (http://oauth.googlecode.com/svn/code/php/) *
* @copyright Campus Notes (C) 2009
*/
/**
* The exception thrown when something bad happens in OAuthClient
*/
class OAuthClientException extends Exception {}
/**
* OAuthClient is a simple sample OAuth Client implementation
* @author Morten Fangel <[email protected]>
*/
class OAuthClient {
private $oauth_consumer;
private $oauth_token;
private $hmac_signature_method;
const REQUEST_URL = 'http://oauth-sandbox.sevengoslings.net/request_token';
const AUTH_URL = 'http://oauth-sandbox.sevengoslings.net/authorize';
const ACCESS_URL = 'http://oauth-sandbox.sevengoslings.net/access_token';
/**
* Create a new OauthClient instance
* @param OAuthConsumer $c Your consumer info
* @param OAuthToken $t Your AccessToken (null if none)
*/
public function __construct( OAuthConsumer $c, OAuthToken $t = null ) {
$this->oauth_consumer = $c;
$this->oauth_token = $t;
$this->hmac_signature_method = $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
}
/**
* Fetches a new RequestToken for you to use..
* @throws CNApiException
* @return OAuthToken
*/
public function getRequestToken() {
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
null,
'GET',
self::REQUEST_URL
);
$token_str = $this->_performRequest($req);
parse_str($token_str, $token_arr);
if( isset($token_arr['oauth_token'], $token_arr['oauth_token_secret']) ) {
return new OAuthToken($token_arr['oauth_token'], $token_arr['oauth_token_secret']);
} else {
return null;
}
}
/**
* Returns the URL you can direct the user to for authorization
* @param OAuthToken $request_token
* @param string $callback_url
* @return string
*/
public function getAuthorizeUrl( OAuthToken $request_token, $callback_url = null ) {
$url = self::AUTH_URL . '?oauth_token=' . $request_token->key;
if( $callback_url ) $url .= '&oauth_callback=' . urlencode($callback_url);
return $url;
}
/**
* Exchanges a RequestToken for a AccessToken
* @param OAuthToken $request_token
* @return OAuthToken
* @throws CNApiException
*/
public function getAccessToken( OAuthToken $request_token ) {
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
$request_token,
'GET',
self::ACCESS_URL
);
$token_str = $this->_performRequest($req, $request_token);
parse_str($token_str, $token_arr);
if( isset($token_arr['oauth_token'], $token_arr['oauth_token_secret']) ) {
return new OAuthToken($token_arr['oauth_token'], $token_arr['oauth_token_secret']);
} else {
return null;
}
}
/**
* Call a method with full 3-legged authorization
* @param string $url
* @param array $params
* @return array
* @throws CNApiException;
*/
public function call( $url, $params ) {
if( !$this->oauth_token ) return array();
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
$this->oauth_token,
'GET',
$url,
$params
);
$str = $this->_performRequest($req);
if( ! $str ) {
throw new OAuthClientException( 'Failed request' );
} else {
return $json;
}
}
/**
* Calls a method using only 2-legged authorization
*/
public function call_two_legged( $url, $params ) {
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
new OAuthToken('', ''),
'GET',
$url,
$params
);
$str = $this->_performRequest($req);
if( ! $str ) {
throw new OAuthClientException( 'Failed request' );
} else {
return $json;
}
}
/**
* Performs a OAuthRequest, returning the response
* You can give a token to force signatures with this
* token. If none given, the token used when creating
* this instance of CampusNotesAPI is used
* @param OAuthRequest $req
* @param OAuthToken $token
* @return string
* @throws CNApiException
*/
private function _performRequest( OAuthRequest $req, OAuthToken $token = null ) {
$token = ($token) ? $token : $this->oauth_token;
$req->sign_request($this->hmac_signature_method, $this->oauth_consumer, $token);
$curl = curl_init();
$params = $req->get_parameters();
foreach( array_keys($params) AS $i )
if( substr($i, 0, 6) == 'oauth_' )
unset($params[$i]);
$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);
}
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
$req->to_header()
));
$rtn = curl_exec($curl);
if( !$rtn ) {
throw new OAuthClientException( curl_error($curl) );
} else if( curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200 ) {
throw new OAuthClientException( $rtn );
} else {
return $rtn;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment