Sample Helper Function for the PHP SDK

Last time, I have shared the idea on writing our own helper functions in developing our Facebook Applications by using the PHP SDK.

Someone has asked me for sample code……Well, the helper functions should be developed to help you in developing application and therefore depends greatly on how you structure your app, whether you have your own application level infrastructure.

These helper functions can be as simple as accessing Facebook data, perform a high level action.  For example, the following helper function can be used to delete the app requests of a user ($requestObjIDs is an array of app request IDs):

function deleteAppRequest($userID, $requestObjIDs) {
	global $facebook;
	if ( !is_numeric($userID) or empty($requestObjIDs) ) { return false; }

	$groupDeleteResult = true;
	foreach ($requestObjIDs as $request_id) {
		try{
			 $delete_success = $facebook->api('/'.$request_id.'_'.$userID, 'DELETE');
			 if (!$delete_success) { $groupDeleteResult = false; }
		} catch (Exception $e) {
			 process_error($e);
		}
	}

	return $groupDeleteResult;
}

In this example, a single Boolean value is returned to indicate whether there is any problem in deleting the app requests.  Based on your need, you can say return an array which contains the app request IDs that cannot be deleted.  You can also say stop the processing whenever an error occur (in the above, it will loop through all the requestObjIDs even when problem occurred in deleting one of them).

Hope this helps.

About takwing

A moderator of the Official Facebook Developer Forum. http://www.takwing.idv.hk/tech/fb_dev/index.php
This entry was posted in Development Tips and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>