B. Facebook


Authentication

The first thing we want to do is get an app key and app secret. This can be acheived by visiting Facebook Apps and creating a new app or using an existing one. We can next plug in both of those to start an authentication process as in Figure 1. And dont forget to replace http://yourwebsite.com/auth with the URL of your website and page you will be handling the Facebook authentication.

Figure 1. Instantiate Auth
$auth = eden('facebook')->auth('[FACEBOOK_KEY]', '[FACEBOOK_SECRET]', 'http://yourwebsite.com/auth');

Figure 2 is used to redirect the user to facebook to continue the authentication.

Figure 2. Get Login URL
echo $auth->getLoginUrl();

If you need more permissions other than Facebook's default, you can generate a special URL with the same method. Facebook has a list of Permissions on their developer site. You can add permissions as in Figure 2a.

Figure 2a. Get Login URL with permissions
echo $auth->getLoginUrl(array('email', 'user_website'));

When the user has authorized our app to use their data, facebook will add an auth code, we can use to transfer to an access token. Figure 3 shows how we can convert that auth code to a token.

Figure 3. Get Token
$access = $auth->getAccess($_GET['code']);
$_SESSION['fb_token'] = $access['access_token'];

An example of the whole entire authentication process can be found in Figure 4.

Figure 4. Authentication
//start session
session_start();
//get auth
$auth = eden('facebook')->auth('[FACEBOOK_KEY]', '[FACEBOOK_SECRET]', 'http://yourwebsite.com/auth');

//if no code and no session
if(!isset($_GET['code']) && !isset($_SESSION['fb_token'])) {
	//redirect to login
	$login = $auth->getLoginUrl();
	
	header('Location: '.$login);
	exit;
}

//Code is returned back from facebook
if(isset($_GET['code'])) {
	//save it to session
	$access = $auth->getAccess($_GET['code']);
	$_SESSION['fb_token'] = $access['access_token'];
}

Graph

Authenticating is probably the hardest part. Once we get a token from Facebook, you are free to use any of the graph methods, given you have permission to do so by Facebook.

Figure 5. Instantiate the Graph
$graph = eden('facebook')->graph($_SESSION['fb_token']);
Figure 6. Graph Methods
Method Arguments Returns Description
$graph->addAlbum($id, $name, $message);
  1. string|int
  2. - the object ID to place the album
  3. string
  4. string
  5. - the album description
int - the album ID Add an album
$graph->addComment($id, $message);
  1. int
  2. - the post ID commenting on
  3. string
int - the comment ID Adds a comment to a post
$graph->attendEvent($id);
  1. int
  2. - the event ID
Eden_Facebook_Graph Attend an event
$graph->createNote($id = 'me', $subject, $message);
  1. int|string
  2. - object ID where to put the note
  3. string
  4. string
int Add a note
$graph->declineEvent($id);
  1. int
  2. - event ID
Eden_Facebook_Graph Decline an event
$graph->event($name, $start, $end);
  1. string
  2. - name of event
  3. string|int
  4. - string date or time format
  5. string|int
  6. - string date or time format
Eden_Facebook_Event Add an event
$graph->getFields($id = 'me', $fields);
  1. string|int
  2. string|array
array Returns specific fields of an object
$graph->getLogoutUrl($redirect);
  1. string
string Returns the logout URL
$graph->getObject($id = 'me', $connection = NULL, array $query = array(), $auth = true);
  1. string|int
  2. string|null
  3. array
  4. bool
array Returns the detail of any object
$graph->getPermissions($id = 'me');
  1. string|int
array Returns user permissions
$graph->getPictureUrl($id = 'me', $token = true);
  1. string|int
  2. bool
string Returns the user's image
$graph->getUser(); none array Returns the user info
$graph->like($id);
  1. int|string
  2. - object ID
array Like an object
$graph->link($url);
  1. string
Eden_Facebook_Link Add a link
$graph->maybeEvent($id);
  1. int
  2. - event ID
Eden_Facebook_Graph Maybe an event
$graph->post($message);
  1. string
Eden_Facebook_Post Returns Facebook Post
$graph->uploadPhoto($albumId, $file, $message = NULL);
  1. int|string
  2. string
  3. string|null
int - photo ID Uploads a file of your album

Publishing a Post

Figure 7. Instantiate Post Publishing
$post = $graph->post('Your Message');
Figure 8. Post Publishing Methods
Method Arguments Returns Description
$post->create(); none Eden_Facebook_Post sends the post to facebook
$post->setDescription($description);
  1. string
Eden_Facebook_Post Sets media description
$post->setIcon($url);
  1. string
Eden_Facebook_Post Sets anicon for this post
$post->setLink($url);
  1. string
Eden_Facebook_Post sets the link to your post
$post->setPicture($url);
  1. string
Eden_Facebook_Post sets the picture to your post
$post->setTitle($title);
  1. string
Eden_Facebook_Post Sets the title of a post
$post->setVideo($url);
  1. string
Eden_Facebook_Post sets the video to your post

Publishing an Event

Figure 9. Instantiate Event Publishing
$event = $graph->event('Event Title', 'March 26, 2017 1:28pm', 'March 26, 2017 2:28pm');
Figure 10. Event Publishing Methods
Method Arguments Returns Description
$event->create(); none int sends the post to facebook
$event->setCity($city);
  1. string
Eden_Facebook_Event Sets the venue city
$event->setCoordinates($latitude, $longitude);
  1. float
  2. float
Eden_Facebook_Event Sets the venue coordinates
$event->setCountry($country);
  1. string
Eden_Facebook_Event Sets the venue country
$event->setDescription($description);
  1. string
Eden_Facebook_Event Sets description
$event->setLocation($location);
  1. string
Eden_Facebook_Event Sets the title of a post
$event->setPrivacyClosed(); none Eden_Facebook_Event Sets privacy to closed
$event->setPrivacyOpen(); none Eden_Facebook_Event Sets privacy to open
$event->setPrivacySecret(); none Eden_Facebook_Event Sets privacy to secret
$event->setState($state);
  1. string
Eden_Facebook_Event Sets the venue state
$event->setStreet($street);
  1. string
Eden_Facebook_Event Sets the venue street

Publish a Link

Figure 11. Instantiate Link Publishing
$link = $graph->link('http://eden.openovate.com/');
Figure 12. Link Publishing Methods
Method Arguments Returns Description
$link->create(); none int sends the post to facebook
$link->setCaption($caption);
  1. string
Eden_Facebook_Link Sets a picture caption
$link->setDescription($description);
  1. string
Eden_Facebook_Link Sets description
$link->setId($id);
  1. int|string
Eden_Facebook_Link Set the profile id
$link->setName($name);
  1. string
Eden_Facebook_Link Sets the link title
$link->setPicture($picture);
  1. string
Eden_Facebook_Link Sets a picture

FQL

Figure 13. Instantiate FQL
$fql = eden('facebook')->fql($_SESSION['fb_token']);

FQL has been designed to match our SQL classes to ease the learning curve. In our FQL you'll find common methods like Figure 14.

Figure 14. FQL Methods
Method Arguments Returns Description
$fql->getRow($table, $name, $value);
  1. string
  2. - table
  3. string
  4. - name
  5. string
  6. - value
array Returns a 1 row result given the column name and the value
$fql->getRows($table, $filters = NULL, array $sort = array(), $start = 0, $range = 0, $index = NULL);
  1. string
  2. - table
  3. array
  4. - filter
  5. array
  6. - sort
  7. int
  8. - start
  9. int
  10. - range
array Returns a list of results given the query parameters
$fql->getRowsCount($table, $filters = NULL);
  1. string
  2. - table
  3. array
  4. - filter
int - | false Returns the number of results given the query parameters
$fql->query($query);
  1. string
  2. - query
array|object Queries the database
$fql->search(); none Eden_Facebook_Search Returns search

As you can see from the figure above we still have our beloved search class. This search class behaves the same way with the exception of that there are no join methods (blame Facebook).

Figure 15. FQL Search Methods
Method Arguments Returns Description
addFilter()
  1. string
  2. string[,string..]
Eden_Facebook_Search Adds filter
addSort($column, $order = self::ASC)
  1. string
  2. string
Eden_Facebook_Search Adds sort
getCollection($key = 'last') none Eden_Facebook_Collection Returns the results in a collection
getRows($key = 'last') none array Returns the array rows
getTotal() none int Returns the total results
group($key)
  1. scalar
Eden_Facebook_Search Stores this search and resets class. Useful for multiple queries.
setColumns($columns)
  1. string[,string..]|array
Eden_Facebook_Search Sets Columns
setPage($page)
  1. int
Eden_Facebook_Search Sets the pagination page
setRange($range)
  1. int
Eden_Facebook_Search Sets the pagination range
setStart($start)
  1. int
Eden_Facebook_Search Sets the pagination start
setTable($table)
  1. string
Eden_Facebook_Search Sets Table

Facebook does provide a way we can join tables and we use the group method from Figure 15 to achieve this in Figure 16.

Figure 16. FQL Searching
$fql->search()
	->setTable('event_member')
	->filterByEid(12345678)
	->group('query1')
	->setTable('profile')
	->addFilter('id IN (SELECT uid FROM #query1)')
	->debug(true)
	->getRows();

We recommend you take a look at the bottom of the FQL documentation for the list of tables and descriptions as well as columns.php for a list of columns for each table.


© 2012 Openovate Labs. All rights reserved.