PageRenderTime 193ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/sample/client/EDAMTest.php

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