For developers integrating the Evernote API into your PHP web applications, we have found there are certain issues you are likely run in to. Firstly when you download, configure and run The Evernote API SDK for PHP, most web servers will cause the script to complain stating that the PECL OAuth Extension must be installed. Installing the PECL OAuth extension is relatively straight-forward if you have SSH command line access to your server.
However, for most people installing the extension is simply not an option. There are major conflicts with the PECL OAuth PHP extension and the OAuth libraries available for download from Google Code SVN. One example of this would be: Installing the PECL OAuth extension prevents the standard OAuth libraries from functioning altogether and this will be an issue for all websites hosted on the same server. Furthermore, you may not have access to the server command line needed to install the extension, or your web host may not allow the extension to be installed for various reasons.
The simple truth is that although the Evernote API PHP SDK states that the PECL PHP OAuth extension is required, the SDK can easily be adapted for use the with standard OAuth library that most developers are akin to working with.
The solution and the source code
You will need…
- To sign up for an Evernote API Key
- To download and unzip the Evernote API SDK
- To download the OAuth-PHP library files hosted on Google Code
- Access to a FTP client.
- Access to a Text editor.
Instructions for setting it up…
- Create a public folder called ‘/evernote/’ on your server, it will run the ‘OAuth dance’ process from here.
- In the Evernote SDK files, browse to the folder: ‘/php/sample/oauth/’ and upload the contents of it to your new ‘evernote’ folder on the server.
- Upload the OAuth-PHP ‘/library/…’ files to a new folder in ‘/evernote/’ called: ‘/oauth/’. So that the file ‘/evernote/oauth/OAuthRequester.php’ will exists publicly on the server.
- Add your API Key and other credentials to the ‘/evernote/config.php’ file and configure the sandbox/production URL’s if desired.
- Time to code. First, replace the ‘/evernote/index.php’ contents with the following code:
EVERNOTE_CONSUMER_KEY,
'consumer_secret' => EVERNOTE_CONSUMER_SECRET,
'server_uri' => EVERNOTE_OAUTH_HOST,
'request_token_uri' => EVERNOTE_REQUEST_TOKEN_URL,
'authorize_uri' => EVERNOTE_AUTHORIZE_URL,
'access_token_uri' => EVERNOTE_ACCESS_TOKEN_URL
);
OAuthStore::instance("Session", $options);
try {
// Get initial OAuth request token
if (empty($_GET["oauth_token"])) {
$getAuthTokenParams = array('oauth_callback' => $siteurl."/evernote");
$tokenResultParams = OAuthRequester::requestRequestToken(EVERNOTE_CONSUMER_KEY, 0, $getAuthTokenParams);
// Redirect to the evernote authorization page, they'll be back soon
header("Location: ".EVERNOTE_AUTHORIZE_URL."?oauth_token=".$tokenResultParams['token']);
} else {
// Get the access token
$oauthToken = $_GET["oauth_token"];
$tokenResultParams = $_GET;
$_SESSION['accessToken'] = $tokenResultParams;
try {
OAuthRequester::requestAccessToken(EVERNOTE_CONSUMER_KEY, $oauthToken, 0, 'POST', $tokenResultParams);
} catch (OAuthException2 $e) {
var_dump($e);
return;
}
// Authorized.
$notebooks = listNotebooks();
print_r($notebooks);
}
}
catch(OAuthException2 $e) {
echo "OAuthException: ".$e->getMessage();
var_dump($e);
}
?>
You will need to modify the ‘/evernote/oauth/OAuthRequester.php’ ‘requestAccessToken()’ function slightly to provide you with the Evernote API access credentials needed to make any future requests to the Evernote API. This information can be saved to a database, however for simplicity we have saved them in session variables. In Evernote, the information you need for future requests is known as a ‘Credentials Identifier’. To set this up, simply add the following 3 lines of code immediately after the line with a call to: ’$store->addServerToken();’:
addServerToken(); add... $_SESSION['edam_shard'] = $token['edam_shard']; $_SESSION['edam_userid'] = $token['edam_userId']; $_SESSION['credentials_identifier'] = $token['oauth_token']; ?>
Lastly, you should replace the listNotebooks() function in ‘/evernote/functions.php’ with this:
listNotebooks($authToken); // Refer to API docs
$result = array();
if(!empty($notebooks)) {
foreach($notebooks as $notebook) {
$result[] = $notebook->name; // Refer to API docs
}
}
// Returns an array of notebooks
return $result;
} catch (edam_error_EDAMSystemException $e) {
if (isset(edam_error_EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error listing notebooks: ' . edam_error_EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error listing notebooks: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (edam_error_EDAMUserException $e) {
if (isset(edam_error_EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error listing notebooks: ' . edam_error_EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error listing notebooks: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (edam_error_EDAMNotFoundException $e) {
if (isset(edam_error_EDAMErrorCode::$__names[$e->errorCode])) {
$lastError = 'Error listing notebooks: ' . edam_error_EDAMErrorCode::$__names[$e->errorCode] . ": " . $e->parameter;
} else {
$lastError = 'Error listing notebooks: ' . $e->getCode() . ": " . $e->getMessage();
}
} catch (Exception $e) {
$lastError = 'Error listing notebooks: ' . $e->getMessage();
}
return $lastError;
}
?>
That’s all there is to it. If you save and upload all of the modified files and navigate your browser to your ‘/evernote/’ folder, it will redirect you to the Evernote authorize page prompting you to login with your Evernote Account. Upon doing so, you will be immediately redirected back to your ‘/evernote/’ folder where you will see your array of Notebooks output in a recursive, human-readable format. You can now do as you please with the array of notebooks in the ‘/evernote/index.php’ file. You can also utilize the same code to develop additional or alternate Evernote API request functionality such as returning lists of notes, tags, creating tags and more.

