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:
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();’:
Lastly, you should replace the listNotebooks() function in ‘/evernote/functions.php’ with this:
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.