Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created September 22, 2011 16:35
Show Gist options
  • Save hubgit/1235269 to your computer and use it in GitHub Desktop.
Save hubgit/1235269 to your computer and use it in GitHub Desktop.
Minimal PHP + OAuth class for interacting with the Mendeley API
<?php
// PHP OAuth class: http://www.php.net/manual/en/book.oauth.php
// register at http://dev.mendeley.com/applications/
define('MENDELEY_CONSUMER_KEY', ''); // edit this
define('MENDELEY_CONSUMER_SECRET', ''); // edit this
class Mendeley {
function __construct(){
$this->oauth = new OAuth(MENDELEY_CONSUMER_KEY, MENDELEY_CONSUMER_SECRET, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI);
$this->oauth->enableDebug(); // for debugging
}
function fetch($url, $params = null, $method = 'GET', $headers = array()) {
try {
$this->oauth->fetch('http://api.mendeley.com/oapi' . $url, $params, constant('OAUTH_HTTP_METHOD_' . $method), $headers);
return json_decode($this->oauth->getLastResponse(), true);
}
catch(OAuthException $e) {
print_r($this->oauth->debugInfo); // for debugging
}
}
// http://apidocs.mendeley.com/home/public-resources/search-details
function document($id, $type = null) {
if ($type == 'doi') $id = str_replace('/', '%2F', $id); // workaround
return $this->fetch('/documents/details/' . rawurlencode($id), array('type' => $type));
}
}
$api = new Mendeley;
$data = $api->document('10.1038/nchem.1140', 'doi'); // an example DOI
print_r($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment