<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: addressbook code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 03:24:38 GMT</pubDate>
    <description>DZone Snippets: addressbook code</description>
    <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>
