Created
September 22, 2011 16:35
-
-
Save hubgit/1235269 to your computer and use it in GitHub Desktop.
Minimal PHP + OAuth class for interacting with the Mendeley API
This file contains hidden or 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 | |
// 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