Posted By

dannymo2 on 03/06/08


Tagged

list api contact addressbook social webmail importer


Versions (?)

Rapleaf Address Book API


 / Published in: PHP
 

URL: http://www.rapleaf.com/developer

Use this API to access names and addresses in a user's webmail (Gmail, AOL, Hotmail, and Yahoo). Make it easy to send friend invites for a social site and import contact lists. Rapleaf maintains up to date code on the importer and never stores passwords.

  1. <?php
  2. /*
  3.  * Helper class for the Rapleaf Address Book API
  4.  * (http://www.rapleaf.com/apidoc/v2/abook)
  5.  * Gets the XML response from the Rapleaf server and parses it into a PHP object.
  6.  *
  7.  * Usage:
  8.  * 1. $abook = new RapleafAbook(api_key[,url]) to initialize
  9.  * 2. $result = $abook->getData(email, password) to query the API for a contact list
  10.  * See the function definitions for details.
  11.  *
  12.  * 03/01/2008
  13.  *
  14.  */
  15.  
  16. class RapleafAbook {
  17. var $url;
  18. var $api_key;
  19. var $status;
  20.  
  21. function RapleafAbook($api_key, $url = 'http://api.rapleaf.com/v2/abook') {
  22. $this->api_key = $api_key;
  23. $this->url = $url;
  24. $this->status = '';
  25. }
  26.  
  27. function getData($email, $pass) {
  28. # assemble post_data string
  29. $post_data = "login=$email&password=$pass";
  30. $response = $this->sendPostRequest($this->url, $post_data);
  31.  
  32. # the return structure
  33. $result = array(
  34. 'status' => '', # HTTP status code returned by the server
  35. 'error' => '', # error message if there are any
  36. 'contacts' => array(), # contact list if request succeeded
  37. );
  38.  
  39. $result['status'] = $this->status;
  40. if ($this->status == '200') { #OK
  41. $result['contacts'] = $this->xmlToObj($response);
  42. } elseif ($this->status == '400') {
  43. $result['error'] = 'The request did not contain all required parameters: '.$response;
  44. } elseif ($this->status == '401') {
  45. $result['error'] = 'API key was not provided or is invalid.';
  46. } elseif ($this->status == '420') {
  47. $result['error'] = 'Login failed.';
  48. } elseif ($this->status == '500') {
  49. $result['error'] = 'There was an unexpected error on our server. This should be very rare and if you see it please contact [email protected]';
  50. } elseif ($this->status == '520') {
  51. $result['error'] = 'There was an error while reading the contacts from the address book.';
  52. }
  53. return $result;
  54. }
  55.  
  56. # Parse the xml response text into an associative array
  57. function xmlToObj($str) {
  58. $xml = simplexml_load_string($str);
  59. $result = array();
  60. foreach ($xml->contact as $contact) {
  61. $result[] = array('name' => (string) $contact['name'], 'email' => (string) $contact['email']);
  62. }
  63. return $result;
  64. }
  65.  
  66. # Returns the xml response on success, sets the error message on failure
  67. function sendPostRequest($url, $post_data) {
  68. $ch = curl_init();
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  70. curl_setopt($ch, CURLOPT_TIMEOUT, 20);
  71. curl_setopt($ch, CURLOPT_URL, $url);
  72. curl_setopt($ch, CURLOPT_POST, 1);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  74. curl_setopt($ch, CURLOPT_HTTPHEADER,
  75. array("Authorization: ".$this->api_key, "Content-Type: application/x-www-form-urlencoded")
  76. );
  77.  
  78. $data = curl_exec($ch);
  79. $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  80. curl_close($ch);
  81. return $data;
  82. }
  83. }
  84. ?>

Report this snippet  

You need to login to post a comment.