Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Dan Scudder http://www.rapleaf.com/developer

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Rapleaf Address Book API in Perl

Retrieve e-mail contacts from several services using the Rapleaf Address Book API.

It accesses the Rapleaf API Web server and executes a request to retrieve the contact list of a given user of either Gmail, Yahoo, Hotmail and AOL.

Returns an associative array with the contacts names and e-mail addresses, as well the HTTP response status and any errors.

   1  
   2  package Rapleaf;
   3  use strict;
   4  
   5  use LWP::UserAgent;
   6  use HTTP::Request;
   7  use XML::Simple;
   8  
   9  sub getData {
  10  	my ($email, $pass, $api_key, $url) = @_;
  11  	my $post_data = "login=$email&password=$pass";
  12  
  13  	$url ||= 'http://api.rapleaf.com/v2/abook';
  14  	my $agent = LWP::UserAgent->new();
  15  	my $request = HTTP::Request->new(POST => $url);
  16  	$request->content($post_data);
  17  	$request->header( 'Authorization' => $api_key );
  18  	my $response;
  19  	$response = $agent->request($request);
  20  	my %result; 
  21  	if($response->code == 200) {
  22  		my $xml = new XML::Simple;
  23  		%result = %{$xml->XMLin($response->content)};
  24  
  25  		# if a single contact if found, XMLin returns a result set of a different format, therefore we need to manually format it
  26  		if ($result{'contact'}->{'name'}) {
  27  			$result{'contact'}->{$result{'contact'}->{'name'}} 
  28  				= {'email'=>$result{'contact'}->{'email'}};
  29  			delete $result{'contact'}->{'name'};
  30  			delete $result{'contact'}->{'email'};
  31  		} 
  32  	} elsif ($response->code == 400) {
  33  		$result{'error'} = 'The request did not contain all required parameters: '.$response;
  34  	} elsif ($response->code == 401) {
  35  		$result{'error'} = 'API key was not provided or is invalid.';
  36  	} elsif ($response->code == 420) {
  37  		$result{'error'} = 'Login failed.';
  38  	} elsif ($response->code == 500) {
  39  		$result{'error'} = 'There was an unexpected error on our server. This should be very rare and if you see it please contact developer@rapleaf.com.';
  40  	} elsif ($response->code == 520) {
  41  		$result{'error'} = 'There was an error while reading the contacts from the address book.';
  42  	}
  43  	
  44  	$result{'status'} = $response->code;
  45  	return \%result;
  46  }
  47  
  48  1;
  49  

Rapleaf Address Book API in PHP

// The Rapleaf Address Book API allows you to access names and addresses of a person's webmail contact list (Gmail, AOL, Hotmail, and Yahoo). You can use this API to send friend invites on a social network, etc.

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