Over a million developers have joined DZone.

Rapleaf Address Book API In Perl

DZone's Guide to

Rapleaf Address Book API In Perl

Free Resource
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.

package Rapleaf;
use strict;

use LWP::UserAgent;
use HTTP::Request;
use XML::Simple;

sub getData {
	my ($email, $pass, $api_key, $url) = @_;
	my $post_data = "login=$email&password=$pass";

	$url ||= 'http://api.rapleaf.com/v2/abook';
	my $agent = LWP::UserAgent->new();
	my $request = HTTP::Request->new(POST => $url);
	$request->header( 'Authorization' => $api_key );
	my $response;
	$response = $agent->request($request);
	my %result; 
	if($response->code == 200) {
		my $xml = new XML::Simple;
		%result = %{$xml->XMLin($response->content)};

		# if a single contact if found, XMLin returns a result set of a different format, therefore we need to manually format it
		if ($result{'contact'}->{'name'}) {
				= {'email'=>$result{'contact'}->{'email'}};
			delete $result{'contact'}->{'name'};
			delete $result{'contact'}->{'email'};
	} elsif ($response->code == 400) {
		$result{'error'} = 'The request did not contain all required parameters: '.$response;
	} elsif ($response->code == 401) {
		$result{'error'} = 'API key was not provided or is invalid.';
	} elsif ($response->code == 420) {
		$result{'error'} = 'Login failed.';
	} elsif ($response->code == 500) {
		$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.';
	} elsif ($response->code == 520) {
		$result{'error'} = 'There was an error while reading the contacts from the address book.';
	$result{'status'} = $response->code;
	return \%result;



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}