forked from joestump/services_facebook
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Connect endpoints. Added get and set for api url in Servi…
…ces_Facebook_Common.
- Loading branch information
jeffhodsdon
committed
Jul 24, 2008
1 parent
e0bb147
commit fc315e0
Showing
5 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
<?php | ||
|
||
/** | ||
* PHP5 interface for Facebook's REST API | ||
* | ||
* PHP version 5.1.0+ | ||
* | ||
* LICENSE: This source file is subject to the New BSD license that is | ||
* available through the world-wide-web at the following URI: | ||
* http://www.opensource.org/licenses/bsd-license.php. If you did not receive | ||
* a copy of the New BSD License and are unable to obtain it through the web, | ||
* please send a note to license@php.net so we can mail you a copy immediately. | ||
* | ||
* @category Services | ||
* @package Services_Facebook | ||
* @author Jeff Hodsdon <jeff@digg.com> | ||
* @author Bill Shupp <hostmaster@shupp.org> | ||
* @copyright 2007-2008 Jeff Hodsdon <jeff@digg.com> | ||
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License | ||
* @version Release: @package_version@ | ||
* @link http://pear.php.net/package/Services_Facebook | ||
*/ | ||
|
||
require_once 'Validate.php'; | ||
|
||
/** | ||
* Facebook Application Interface | ||
* | ||
* <code> | ||
* <?php | ||
* require_once 'Services/Facebook.php'; | ||
* $api = new Services_Facebook(); | ||
* $app = $api->connect->(''); | ||
* ?> | ||
* </code> | ||
* | ||
* @category Services | ||
* @package Services_Facebook | ||
* @author Jeff Hodsdon <jeff@digg.com> | ||
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License | ||
* @version Release: @package_version@ | ||
* @link http://wiki.developers.facebook.com | ||
*/ | ||
class Services_Facebook_Connect extends Services_Facebook_Common | ||
{ | ||
|
||
/** | ||
* Construct | ||
* | ||
* Various tasks that should be ran before non-static methods | ||
* | ||
* @access public | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
if (!function_exists('json_encode')) { | ||
throw new Services_Facebook_Exception('PHP Function ' . | ||
'json_encode() is required for Services_Facebook_Connect ' . | ||
' methods.'); | ||
} | ||
} | ||
|
||
/** | ||
* Register Users | ||
* | ||
* The accounts array may hold up to 1,000 accounts. Each account should hold | ||
* these array keys: (account_id and account_url are optional) | ||
* | ||
* <code> | ||
* | ||
* // Hash the emails | ||
* $hash1 = Services_Facebook_Connect::hashEmail('joe@example.com'); | ||
* $hash2 = Services_Facebook_Connect::hashEmail('jeff@example.com'); | ||
* | ||
* $accounts = array(); | ||
* | ||
* $accounts[] = array( | ||
* 'email_hash' => $hash1, | ||
* 'account_id' => 12345678, | ||
* 'account_url' => 'http://example.com/users?id=12345678' | ||
* ) | ||
* | ||
* $accounts[] = array( | ||
* 'email_hash' => $hash2, | ||
* ) | ||
* | ||
* $connect = Services_Facebook::factory('Connect'); | ||
* $result = $connect->registerUsers($accounts); | ||
* </code> | ||
* | ||
* @param array $accounts Information about accounts | ||
* | ||
* @access public | ||
* @throws Services_Facebook_Exception If emash_hash is missing or | ||
* another field was passed in that is not supported. | ||
* @return object SimpleXML object from sendRequest() | ||
*/ | ||
public function registerUsers(array $accounts) | ||
{ | ||
$fields = array( | ||
'email_hash', | ||
'account_id', | ||
'account_url' | ||
); | ||
|
||
foreach ($accounts as $account) { | ||
if (empty($account['email_hash'])) { | ||
throw new Services_Facebook_Exception('email_hash is ' . | ||
'required in each account map passed to ' . | ||
'Services_Facebook_Connect::registerUsers()'); | ||
} | ||
|
||
$keys = array_keys($account); | ||
foreach ($keys as $key) { | ||
if (!in_array($key, $fields)) { | ||
throw new Services_Facebook_Exception('Field ' . $key . | ||
' is not supported.'); | ||
} | ||
} | ||
} | ||
|
||
return $this->sendRequest('connect.registerUsers', array( | ||
'accounts' => json_encode($accounts) | ||
)); | ||
} | ||
|
||
/** | ||
* unregisterUsers | ||
* | ||
* This method allows a site to unregister a connected account. You should | ||
* call this method if the user deletes his account on your site. | ||
* | ||
* | ||
* <code> | ||
* $hashes = array(); | ||
* $hashes[] = Services_Facebook_Connect::hashEmail('joe@example.com'); | ||
* $hashes[] = Services_Facebook_Connect::hashEmail('jeff@example.com'); | ||
* | ||
* $connect = new Services_Facebook::factory('Connect'); | ||
* $result = $connect->unregisterUsers($hashes); | ||
* </code> | ||
* | ||
* @param array $emailHashes An array of email_hashes to unregister | ||
* | ||
* @access public | ||
* @throws Services_Facebook_Exception if json_decode() is not available | ||
* @return object SimpleXML object from sendRequest() | ||
*/ | ||
public function unregisterUsers(array $emailHashes) | ||
{ | ||
return $this->sendRequest('connect.unregisterUsers', array( | ||
'email_hashes' => json_encode($emailHashes) | ||
)); | ||
} | ||
|
||
/** | ||
* hashEmail | ||
* | ||
* @param string $email Email to hash | ||
* | ||
* @static | ||
* @access public | ||
* @return string Hashed email address | ||
* @throws Services_Facebook_Exception | ||
* @see http://www.php.net/crc32 | ||
* @see http://www.php.net/md5 | ||
*/ | ||
static public function hashEmail($email) | ||
{ | ||
if (!Validate::email($email)) { | ||
throw new Services_Facebook_Exception('Invalid email address passed to' | ||
. ' Services_Facebook_Connect::hashEmail()'); | ||
} | ||
|
||
$email = strtolower(trim($email)); | ||
$crc32 = sprintf("%u", crc32($email)); | ||
$md5 = md5($email); | ||
|
||
return $crc32 . '_' . $md5; | ||
} | ||
|
||
/** | ||
* hashEmails | ||
* | ||
* @param array $emails Emails to hash | ||
* | ||
* @static | ||
* @access public | ||
* @return array Hashed emails | ||
*/ | ||
static public function hashEmails(array $emails) | ||
{ | ||
foreach ($emails as &$email) { | ||
$email = self::hashEmail($email); | ||
} | ||
|
||
return $emails; | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--TEST-- | ||
Services_Facebook_Connect::hashEmail() | ||
--FILE-- | ||
<?php | ||
|
||
require_once 'tests-config.php'; | ||
require_once 'Services/Facebook/Connect.php'; | ||
|
||
try { | ||
|
||
$email = 'mary@example.com'; | ||
$hash = Services_Facebook_Connect::hashEmail($email); | ||
|
||
var_dump($hash); | ||
|
||
} catch (Services_Facebook_Exception $e) { | ||
echo $e->getLastCall() . "\n"; | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
string(43) "4228600737_c96da02bba97aedfd26136e980ae3761" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
--TEST-- | ||
Services_Facebook_Connect::unregisterUsers() | ||
--FILE-- | ||
<?php | ||
|
||
require_once 'tests-config.php'; | ||
require_once 'Services/Facebook/Connect.php'; | ||
|
||
try { | ||
|
||
$emails = array( | ||
'mary@example.com', | ||
'jeff@foo.com', | ||
'foo@blah.com' | ||
); | ||
|
||
foreach ($emails as &$email) { | ||
$email = Services_Facebook_Connect::hashEmail($email); | ||
} | ||
|
||
$api = new Services_Facebook(); | ||
$result = $api->connect->unregisterUsers($emails); | ||
|
||
var_dump($result); | ||
|
||
} catch (Services_Facebook_Exception $e) { | ||
echo $e->getLastCall() . "\n"; | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
http://api.facebook.com/restserver.php | ||
Invalid API key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--TEST-- | ||
Services_Facebook_Connect::registerUsers() | ||
--FILE-- | ||
<?php | ||
|
||
require_once 'tests-config.php'; | ||
require_once 'Services/Facebook/Connect.php'; | ||
|
||
try { | ||
|
||
$accounts = array(); | ||
$accounts[] = array( | ||
'email_hash' => Services_Facebook_Connect::hashEmail('foo@bar.com'), | ||
'account_id' => 42, | ||
'account_url' => 'http://example.com/foo' | ||
); | ||
|
||
$accounts[] = array( | ||
'email_hash' => Services_Facebook_Connect::hashEmail('blah@foo.com'), | ||
); | ||
|
||
$api = new Services_Facebook(); | ||
$result = $api->connect->unregisterUsers($accounts); | ||
|
||
var_dump($result); | ||
|
||
} catch (Services_Facebook_Exception $e) { | ||
echo $e->getLastCall() . "\n"; | ||
echo $e->getMessage(); | ||
} | ||
|
||
?> | ||
--EXPECT-- | ||
http://api.facebook.com/restserver.php | ||
Invalid API key |