<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Dannymo2's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 22:56:08 GMT</pubDate>
    <description>DZone Snippets: Dannymo2's Code Snippets</description>
    <item>
      <title>Rapleaf Address Book API in Perl</title>
      <link>http://snippets.dzone.com/posts/show/5232</link>
      <description>Retrieve e-mail contacts from several services using the Rapleaf Address Book API.&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;Returns an associative array with the contacts names and e-mail addresses, as well the HTTP response status and any errors.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package Rapleaf;&lt;br /&gt;use strict;&lt;br /&gt;&lt;br /&gt;use LWP::UserAgent;&lt;br /&gt;use HTTP::Request;&lt;br /&gt;use XML::Simple;&lt;br /&gt;&lt;br /&gt;sub getData {&lt;br /&gt;	my ($email, $pass, $api_key, $url) = @_;&lt;br /&gt;	my $post_data = "login=$email&amp;password=$pass";&lt;br /&gt;&lt;br /&gt;	$url ||= 'http://api.rapleaf.com/v2/abook';&lt;br /&gt;	my $agent = LWP::UserAgent-&gt;new();&lt;br /&gt;	my $request = HTTP::Request-&gt;new(POST =&gt; $url);&lt;br /&gt;	$request-&gt;content($post_data);&lt;br /&gt;	$request-&gt;header( 'Authorization' =&gt; $api_key );&lt;br /&gt;	my $response;&lt;br /&gt;	$response = $agent-&gt;request($request);&lt;br /&gt;	my %result; &lt;br /&gt;	if($response-&gt;code == 200) {&lt;br /&gt;		my $xml = new XML::Simple;&lt;br /&gt;		%result = %{$xml-&gt;XMLin($response-&gt;content)};&lt;br /&gt;&lt;br /&gt;		# if a single contact if found, XMLin returns a result set of a different format, therefore we need to manually format it&lt;br /&gt;		if ($result{'contact'}-&gt;{'name'}) {&lt;br /&gt;			$result{'contact'}-&gt;{$result{'contact'}-&gt;{'name'}} &lt;br /&gt;				= {'email'=&gt;$result{'contact'}-&gt;{'email'}};&lt;br /&gt;			delete $result{'contact'}-&gt;{'name'};&lt;br /&gt;			delete $result{'contact'}-&gt;{'email'};&lt;br /&gt;		} &lt;br /&gt;	} elsif ($response-&gt;code == 400) {&lt;br /&gt;		$result{'error'} = 'The request did not contain all required parameters: '.$response;&lt;br /&gt;	} elsif ($response-&gt;code == 401) {&lt;br /&gt;		$result{'error'} = 'API key was not provided or is invalid.';&lt;br /&gt;	} elsif ($response-&gt;code == 420) {&lt;br /&gt;		$result{'error'} = 'Login failed.';&lt;br /&gt;	} elsif ($response-&gt;code == 500) {&lt;br /&gt;		$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.';&lt;br /&gt;	} elsif ($response-&gt;code == 520) {&lt;br /&gt;		$result{'error'} = 'There was an error while reading the contacts from the address book.';&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	$result{'status'} = $response-&gt;code;&lt;br /&gt;	return \%result;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;1;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 14 Mar 2008 14:39:58 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5232</guid>
      <author>dannymo2 (Dan Scudder)</author>
    </item>
    <item>
      <title>Rapleaf Address Book API in PHP</title>
      <link>http://snippets.dzone.com/posts/show/5202</link>
      <description>// 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. &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;/*&lt;br /&gt; * Helper class for the Rapleaf Address Book API&lt;br /&gt; * 		(http://www.rapleaf.com/apidoc/v2/abook)&lt;br /&gt; * Gets the XML response from the Rapleaf server and parses it into a PHP object.&lt;br /&gt; *&lt;br /&gt; * Usage: &lt;br /&gt; * 	1. $abook = new RapleafAbook(api_key[,url]) to initialize&lt;br /&gt; *  2. $result = $abook-&gt;getData(email, password) to query the API for a contact list&lt;br /&gt; *  See the function definitions for details.&lt;br /&gt; *  &lt;br /&gt; * 03/01/2008&lt;br /&gt; *&lt;br /&gt; */&lt;br /&gt; &lt;br /&gt;class RapleafAbook {&lt;br /&gt;	var $url;&lt;br /&gt;	var $api_key;&lt;br /&gt;	var $status; &lt;br /&gt;&lt;br /&gt;	function RapleafAbook($api_key, $url = 'http://api.rapleaf.com/v2/abook') {&lt;br /&gt;		$this-&gt;api_key = $api_key;&lt;br /&gt;		$this-&gt;url = $url;&lt;br /&gt;		$this-&gt;status = '';&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	function getData($email, $pass) {&lt;br /&gt;		# assemble post_data string&lt;br /&gt;		$post_data = "login=$email&amp;password=$pass";&lt;br /&gt;		$response = $this-&gt;sendPostRequest($this-&gt;url, $post_data);&lt;br /&gt;    &lt;br /&gt;		# the return structure&lt;br /&gt;		$result = array(&lt;br /&gt;			'status'   =&gt; '',  # HTTP status code returned by the server&lt;br /&gt;			'error'    =&gt; '',  # error message if there are any&lt;br /&gt;			'contacts' =&gt; array(),  # contact list if request succeeded&lt;br /&gt;		);&lt;br /&gt;		&lt;br /&gt;		$result['status'] = $this-&gt;status;&lt;br /&gt;		if ($this-&gt;status == '200') { #OK&lt;br /&gt;			$result['contacts'] = $this-&gt;xmlToObj($response);&lt;br /&gt;		} elseif ($this-&gt;status == '400') {&lt;br /&gt;			$result['error'] = 'The request did not contain all required parameters: '.$response;&lt;br /&gt;		} elseif ($this-&gt;status == '401') {&lt;br /&gt;			$result['error'] = 'API key was not provided or is invalid.';&lt;br /&gt;		} elseif ($this-&gt;status == '420') {&lt;br /&gt;			$result['error'] = 'Login failed.';&lt;br /&gt;		} elseif ($this-&gt;status == '500') {&lt;br /&gt;			$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.';&lt;br /&gt;		} elseif ($this-&gt;status == '520') {&lt;br /&gt;			$result['error'] = 'There was an error while reading the contacts from the address book.';&lt;br /&gt;		}&lt;br /&gt;		return $result;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	# Parse the xml response text into an associative array&lt;br /&gt;	function xmlToObj($str) {&lt;br /&gt;		$xml = simplexml_load_string($str);&lt;br /&gt;		$result = array();&lt;br /&gt;		foreach ($xml-&gt;contact as $contact) {&lt;br /&gt;			$result[] = array('name' =&gt; (string) $contact['name'], 'email' =&gt; (string) $contact['email']);&lt;br /&gt;		}&lt;br /&gt;		return $result;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	# Returns the xml response on success, sets the error message on failure&lt;br /&gt;	function sendPostRequest($url, $post_data) {&lt;br /&gt;		$ch = curl_init();&lt;br /&gt;		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);&lt;br /&gt;		curl_setopt($ch, CURLOPT_TIMEOUT, 20);&lt;br /&gt;		curl_setopt($ch, CURLOPT_URL, $url);&lt;br /&gt;		curl_setopt($ch, CURLOPT_POST, 1);&lt;br /&gt;		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);&lt;br /&gt;		curl_setopt($ch, CURLOPT_HTTPHEADER, &lt;br /&gt;			array("Authorization: ".$this-&gt;api_key, "Content-Type: application/x-www-form-urlencoded")&lt;br /&gt;		);&lt;br /&gt;		&lt;br /&gt;		$data = curl_exec($ch);&lt;br /&gt;		$this-&gt;status = curl_getinfo($ch, CURLINFO_HTTP_CODE);&lt;br /&gt;		curl_close($ch);&lt;br /&gt;		return $data;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 06 Mar 2008 20:55:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5202</guid>
      <author>dannymo2 (Dan Scudder)</author>
    </item>
  </channel>
</rss>
