/sample/client/EDAMTest.php
PHP | 140 lines | 79 code | 23 blank | 38 comment | 9 complexity | 2d4aa0a6ed9c7bb42eac8f13eb3daae1 MD5 | raw file
1<?php 2// 3// A simple command-line Evernote API demo script that lists all notebooks in 4// the user's account and creates a simple test note in the default notebook. 5// 6// Before running this sample, you must fill in your Evernote developer token. 7// 8// To run: 9// php EDAMTest.php 10// 11 12// Import the classes that we're going to be using 13use EDAM\Types\Data, EDAM\Types\Note, EDAM\Types\Resource, EDAM\Types\ResourceAttributes; 14use EDAM\Error\EDAMUserException, EDAM\Error\EDAMErrorCode; 15use Evernote\Client; 16 17ini_set("include_path", ini_get("include_path") . PATH_SEPARATOR . "../../lib" . PATH_SEPARATOR); 18require_once 'autoload.php'; 19 20require_once 'Evernote/Client.php'; 21 22require_once 'packages/Errors/Errors_types.php'; 23require_once 'packages/Types/Types_types.php'; 24require_once 'packages/Limits/Limits_constants.php'; 25 26// A global exception handler for our program so that error messages all go to the console 27function en_exception_handler($exception) 28{ 29 echo "Uncaught " . get_class($exception) . ":\n"; 30 if ($exception instanceof EDAMUserException) { 31 echo "Error code: " . EDAMErrorCode::$__names[$exception->errorCode] . "\n"; 32 echo "Parameter: " . $exception->parameter . "\n"; 33 } elseif ($exception instanceof EDAMSystemException) { 34 echo "Error code: " . EDAMErrorCode::$__names[$exception->errorCode] . "\n"; 35 echo "Message: " . $exception->message . "\n"; 36 } else { 37 echo $exception; 38 } 39} 40set_exception_handler('en_exception_handler'); 41 42// Real applications authenticate with Evernote using OAuth, but for the 43// purpose of exploring the API, you can get a developer token that allows 44// you to access your own Evernote account. To get a developer token, visit 45// https://sandbox.evernote.com/api/DeveloperToken.action 46$authToken = "your developer token"; 47 48if ($authToken == "your developer token") { 49 print "Please fill in your developer token\n"; 50 print "To get a developer token, visit https://sandbox.evernote.com/api/DeveloperToken.action\n"; 51 exit(1); 52} 53 54// Initial development is performed on our sandbox server. To use the production 55// service, change 'sandbox' => true to 'sandbox' => false and replace your 56// developer token above with a token from 57// https://www.evernote.com/api/DeveloperToken.action 58$client = new Client(array('token' => $authToken, 'sandbox' => true)); 59 60$userStore = $client->getUserStore(); 61 62// Connect to the service and check the protocol version 63$versionOK = 64 $userStore->checkVersion("Evernote EDAMTest (PHP)", 65 $GLOBALS['EDAM_UserStore_UserStore_CONSTANTS']['EDAM_VERSION_MAJOR'], 66 $GLOBALS['EDAM_UserStore_UserStore_CONSTANTS']['EDAM_VERSION_MINOR']); 67print "Is my Evernote API version up to date? " . $versionOK . "\n\n"; 68if ($versionOK == 0) { 69 exit(1); 70} 71 72$noteStore = $client->getNoteStore(); 73 74// List all of the notebooks in the user's account 75$notebooks = $noteStore->listNotebooks(); 76print "Found " . count($notebooks) . " notebooks\n"; 77foreach ($notebooks as $notebook) { 78 print " * " . $notebook->name . "\n"; 79} 80 81print"\nCreating a new note in the default notebook\n\n"; 82 83// To create a new note, simply create a new Note object and fill in 84// attributes such as the note's title. 85$note = new Note(); 86$note->title = "Test note from EDAMTest.php"; 87 88// To include an attachment such as an image in a note, first create a Resource 89// for the attachment. At a minimum, the Resource contains the binary attachment 90// data, an MD5 hash of the binary data, and the attachment MIME type. It can also 91// include attributes such as filename and location. 92$filename = "enlogo.png"; 93$image = fread(fopen($filename, "rb"), filesize($filename)); 94$hash = md5($image, 1); 95 96$data = new Data(); 97$data->size = strlen($image); 98$data->bodyHash = $hash; 99$data->body = $image; 100 101$resource = new Resource(); 102$resource->mime = "image/png"; 103$resource->data = $data; 104$resource->attributes = new ResourceAttributes(); 105$resource->attributes->fileName = $filename; 106 107// Now, add the new Resource to the note's list of resources 108$note->resources = array( $resource ); 109 110// To display the Resource as part of the note's content, include an <en-media> 111// tag in the note's ENML content. The en-media tag identifies the corresponding 112// Resource using the MD5 hash. 113$hashHex = md5($image, 0); 114 115// The content of an Evernote note is represented using Evernote Markup Language 116// (ENML). The full ENML specification can be found in the Evernote API Overview 117// at http://dev.evernote.com/documentation/cloud/chapters/ENML.php 118$note->content = 119 '<?xml version="1.0" encoding="UTF-8"?>' . 120 '<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">' . 121 '<en-note>Here is the Evernote logo:<br/>' . 122 '<en-media type="image/png" hash="' . $hashHex . '"/>' . 123 '</en-note>'; 124 125// When note titles are user-generated, it's important to validate them 126$len = strlen($note->title); 127$min = $GLOBALS['EDAM_Limits_Limits_CONSTANTS']['EDAM_NOTE_TITLE_LEN_MIN']; 128$max = $GLOBALS['EDAM_Limits_Limits_CONSTANTS']['EDAM_NOTE_TITLE_LEN_MAX']; 129$pattern = '#' . $GLOBALS['EDAM_Limits_Limits_CONSTANTS']['EDAM_NOTE_TITLE_REGEX'] . '#'; // Add PCRE delimiters 130if ($len < $min || $len > $max || !preg_match($pattern, $note->title)) { 131 print "\nInvalid note title: " . $note->title . '\n\n'; 132 exit(1); 133} 134 135// Finally, send the new note to Evernote using the createNote method 136// The new Note object that is returned will contain server-generated 137// attributes such as the new note's unique GUID. 138$createdNote = $noteStore->createNote($note); 139 140print "Successfully created a new note with GUID: " . $createdNote->guid . "\n";