1<?php 2define('HORDE_BASE', '/private/var/www/html/horde'); 3require_once HORDE_BASE . '/lib/base.php'; 4 5// To call Facebook API methods, you will need to set up the application in 6// Facebook, and obtain both the api_key and the app_secret. 7// See: 8// http://developers.facebook.com/get_started.php?tab=tutorial 9 10$apikey = 'xxx'; //CHANGE THIS 11$secret = 'xxx'; //CHANGE THIS 12 13/** 14 * Horde_Service_Facebook *requires* an http_client, http_request objects 15 * and a Horde_Log_Logger object 16 */ 17$context = array('http_client' => new Horde_Http_Client(), 18 'http_request' => new Horde_Controller_Request_Http()); 19 20/** Create the facebook object **/ 21$facebook = new Horde_Service_Facebook($apikey, $secret, $context); 22 23/** 24 * Authenticating and logging into a Facebook app from an external site is 25 * a complicated and multi-stage process. For these examples, we are assuming 26 * that we have authenticated the application and are either already logged into 27 * Facebook or we have authorized 'offline_access'. 28 */ 29 30/** 31 * If we have a valid cookie, this will know about it. This method should also 32 * be called both after the user has authorized the application and again after 33 * the user has (optionally) authorized infinite sessions (offline_access). Then 34 * you would obtain the infinite session_key by calling auth->getSessionKey() and 35 * storing the results as you will not be able to retrieve it from FB again. 36 * This is the *only* way to obtain the session key. 37 */ 38//$facebook->auth->validateSession(); 39 40// Current uid can be obtained with: 41//$uid = $facebook->auth->getUser(); 42 43/** session_key, if you need it, can be obtained via: **/ 44//$sid = $facebook->auth->getSessionKey(); 45 46/** 47 * Otherwise, you would use uid and session_key from prefs or other local 48 * storage and set up the session by calling setUser(). This is how you would 49 * need to do this when utilizing an infinite session_key, since FB will only 50 * send the infinite session_key to you one time only - it's up to client code 51 * to store it. 52 */ 53 $fbp = unserialize($prefs->getValue('facebook')); 54 $uid = $fbp['uid']; 55 $sid = $fbp['sid']; 56 $facebook->auth->setUser($uid, $sid, 0); 57 58 59/** Use a FQL query to get some friend info **/ 60$result = $facebook->fql->run('SELECT name, status FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = ' . $uid . ')'); 61var_dump($result); 62 63/** Similar can be done as so using individual api calls...but takes a *long* time **/ 64//$friends = $facebook->friends_get(); 65// foreach ($friends as $friend) { 66// $personArray = $facebook->users_getInfo($friend, 'name'); 67// $person[] = $personArray[0]; 68// } 69// 70// foreach ($person as $f) { 71// echo ' ' . $f['name'] . '<br />'; 72// } 73 74/** Calling code that requires extended permissions **/ 75//try { 76// // Set your Facebook status (requires 'status_update' extended perm) 77// $facebook->users->setStatus('is testing my Horde_Service_Facebook client library code...again.'); 78//} catch (Horde_Service_Facebook_Exception $e) { 79// // Check that we failed because of insufficient app permissions. 80// // then redirect if needed... 81// if ($e->getCode() == Horde_Service_Facebook_ErrorCodes::API_EC_PERMISSION_STATUS_UPDATE) { 82// // Don't have status_update...tell user/provide link to authorize page etc... 83 // You can get the link to the authorize page like this: 84// $facebook->auth->getExtendedPermUrl( 85// Horde_Service_Facebook_Auth::EXTEND_PERMS_STATUSUPDATE, 86// 'http://yourcallbackurl.com'); 87// } else { 88// // Something else 89// echo $e->getMessage(); 90// } 91//} 92 93/** 94 * Alternatively, you could check for the necessary perms first, but IMO, it's 95 * more effecient to get the error since checking the perms and then performing 96 * the action require two round trips to the server. 97 */ 98//$hasPerm = $facebook->users->hasAppPermissions('status_update'); 99//if ($hasPerm) { 100// //..... 101//} 102 103 104/** 105 * Batch mode. 106 * When calling in batch mode, you must assign the results of the method calls 107 * as a reference so when run() is called, you still have a handle to the 108 * results. 109 */ 110//$facebook->batchBegin(); 111//$notifications = &$facebook->notifications->get(); 112//$friends = &$facebook->friends->get(); 113//$facebook->batchEnd(); 114//var_dump($friends); 115//var_dump($notifications); 116 117/** 118 * View a user's pictures. $uid should be the user id whose albums you want to 119 * retrieve. (Permissions permitting, of course) 120 */ 121//$albums = $facebook->photos->getAlbums($uid); 122//var_dump($albums); 123//$images = $facebook->photos->get('', $albums[0]['aid']); 124//var_dump($images); 125 126 127/** 128 * Request the raw JSON (or XML) data 129 */ 130//$facebook->dataFormat = Horde_Service_Facebook::DATA_FORMAT_JSON; 131//$results = $facebook->photos->getAlbums($uid); 132//var_dump($results); 133 134/** 135 * Upload a photo 136 */ 137$path = "/Users/mrubinsk/Desktop/horde_fb.jpg"; 138$results = $facebook->photos->upload($path); 139var_dump($results);