/ 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.
Expand |
Embed | Plain Text
<?php /* * Helper class for the Rapleaf Address Book API * (http://www.rapleaf.com/apidoc/v2/abook) * Gets the XML response from the Rapleaf server and parses it into a PHP object. * * Usage: * 1. $abook = new RapleafAbook(api_key[,url]) to initialize * 2. $result = $abook->getData(email, password) to query the API for a contact list * See the function definitions for details. * * 03/01/2008 * */ class RapleafAbook { var $url; var $api_key; var $status; function RapleafAbook($api_key, $url = 'http://api.rapleaf.com/v2/abook') { $this->api_key = $api_key; $this->url = $url; $this->status = ''; } function getData($email, $pass) { # assemble post_data string $post_data = "login=$email&password=$pass"; $response = $this->sendPostRequest($this->url, $post_data); # the return structure 'status' => '', # HTTP status code returned by the server 'error' => '', # error message if there are any ); $result['status'] = $this->status; if ($this->status == '200') { #OK $result['contacts'] = $this->xmlToObj($response); } elseif ($this->status == '400') { $result['error'] = 'The request did not contain all required parameters: '.$response; } elseif ($this->status == '401') { $result['error'] = 'API key was not provided or is invalid.'; } elseif ($this->status == '420') { $result['error'] = 'Login failed.'; } elseif ($this->status == '500') { $result['error'] = 'There was an unexpected error on our server. This should be very rare and if you see it please contact [email protected]'; } elseif ($this->status == '520') { $result['error'] = 'There was an error while reading the contacts from the address book.'; } return $result; } # Parse the xml response text into an associative array function xmlToObj($str) { $xml = simplexml_load_string($str); foreach ($xml->contact as $contact) { } return $result; } # Returns the xml response on success, sets the error message on failure function sendPostRequest($url, $post_data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 20); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_HTTPHEADER, ); $data = curl_exec($ch); $this->status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $data; } } ?>
You need to login to post a comment.
