Skip to content

Instantly share code, notes, and snippets.

@fangel
Created March 4, 2010 18:30
Show Gist options
  • Save fangel/321992 to your computer and use it in GitHub Desktop.
Save fangel/321992 to your computer and use it in GitHub Desktop.
<?php
/**
* A Client for Campus Notes' (http://getcampusnotes.com) API
*
* @requires OAuth-php (http://oauth.googlecode.com/svn/code/php/) *
* @copyright Campus Notes (C) 2009
*/
/**
* The exception thrown when something bad happens in CampusNotesAPI
*/
class CNApiException extends Exception {}
/**
* CampusNotesAPI is the main class. It contains methods
* for getting the relevant tokens, and for querying the
* API
* @author Morten Fangel <fangel@sevengoslings.net>
*/
class CampusNotesAPI {
private $oauth_consumer;
private $oauth_token;
private $hmac_signature_method;
const REQUEST_URL = 'http://127.0.0.1/~fangel/campusnotes/api/request_token';
const AUTH_URL = 'http://127.0.0.1/~fangel/campusnotes/api/authorize';
const ACCESS_URL = 'http://127.0.0.1/~fangel/campusnotes/api/access_token';
const API_URL = 'http://127.0.0.1/~fangel/campusnotes/api/json';
/**
* Create a new CampusNotesAPI 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( $callback = 'oob' ) {
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
null,
'GET',
self::REQUEST_URL
);
$req->set_parameter('oauth_callback', $callback);
$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 ) {
$url = self::AUTH_URL . '?oauth_token=' . $request_token->key;
return $url;
}
/**
* Exchanges a RequestToken for a AccessToken
* @param OAuthToken $request_token
* @return OAuthToken
* @throws CNApiException
*/
public function getAccessToken( OAuthToken $request_token, $verifier ) {
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
$request_token,
'GET',
self::ACCESS_URL
);
$req->set_parameter('oauth_verifier', $verifier);
$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 at CampusNotes
* @param string $method
* @param array $params
* @return array
* @throws CNApiException;
*/
public function call( $method, $params ) {
if( !$this->oauth_token ) return array();
$params['method'] = $method;
$req = OAuthRequest::from_consumer_and_token(
$this->oauth_consumer,
$this->oauth_token,
'GET',
self::API_URL,
$params
);
$json_str = $this->_performRequest($req);
$json = json_decode($json_str);
if( isset($json->error) ) {
throw new CNApiException( $json->msg );
} 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 CNApiException( curl_error($curl) );
} else if( curl_getinfo($curl, CURLINFO_HTTP_CODE) != 200 ) {
throw new CNApiException( $rtn );
} else {
return $rtn;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment