Is there a way to hook the facebook callback handling? As I understand, when someone posts a comment using fb:comments, facebook calls our callback URL with a few parameters, including the xid of the comments. I want to use this to award users points for commenting with something like the following:

	if (isset($_REQUEST['fb_sig_xid']))
	//do some cool stuff


mikerasmussen’s picture

err...I think it should be:

    if (isset($_POST['fb_sig_xid']))
    //do some cool stuff
Dave Cohen’s picture

If I were doing this, I'd use the callbackurl attribute, and set it to something like
url(FB_APP_EVENT_PATH . '/my_comment_event')

Then, implement hook_fb($op, $data, &$return). $op will be FB_APP_OP_EVENT, and $data will contain $data['fb'], $data['fb_app'] and $data['event_type'].

hook_fb will be called for other $ops and other events, like when a new user authorizes, so check that $data['event_type'] == 'my_comment_event' before handling the event. The $return parameter does not matter for FB_APP_OP_EVENT.

Another option would be for you to implement your own callback, using hook_menu() and so on. Please let us know whether this works or what you end up with.

mikerasmussen’s picture

ok, I am stumped again. As it turns out, this data is not passed back via the callback, but rather you need to register a javascript handler to listen for a comment post:

function() {
	alert("user " + comment.user + " wrote: " +; 

This works when I add it to the page, but now I have the variable I need "comment.user" in javascript, how the heck do I call my PHP function with this? I looked at some AJAX libraries like PHPLiveX that claim to make it possible to call PHP from within javascript, but I couldn't get it to work for whatever reason....

mikerasmussen’s picture

ok, I got this working in the following way. I had to add the following to the bottom of my page.tpl.php for my theme (there is probably a more elegant way to add this):

 FB_RequireFeatures(["Comments"], function() {
	//alert("user: " + comment.user + " wrote: " +; 
    $.get('/comments/award/' + comment.user, null, null);
	//alert("crapper " + comment.user + " wrote: " +; 

Notice the call to $.get with the comment.user as an "argument" attached to the path.

Then I added the following in my custom module source:

function award_user_comment_points($userinput) {
	global $fb;	  
	$user = fb_user_get_local_user($userinput,$fb);
		  'uid' => $user->uid,
		  'points' => 10

and the following _menu hook that registers the above function as the callback for the /comments/award url...

function dffcustom_menu() {
	  $items['comments/award'] = array(
    'page callback' => 'award_user_comment_points',
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
  return $items;


Only thing is that I have to refresh to get it to show up, so I am doing something wrong still, but for now I think I can live with that. Any ideas?

Dave Cohen’s picture

Javascript in general is a pain... and facebook tends to make it only more so. So, the fact that you got anything working... pat yourself on the back.

You may be able to clean things up by searching Drupal's docs about how to add javascript, and looking to other modules for examples.

It's bad form to add javascript to page.tpl.php. Instead, look at fb_connect.module as an example. See fb_connect_fb() as an example of hook_fb() that adds some javascript to every page. Your module could do something very similar. For example, fb_connect_require_feature('Comments') would get that require features function. Sorry, I haven't worked with that specifically, yet. So I encourage you to continue sharing here whatever you end up with.

eosrei’s picture

Status: Active » Closed (fixed)

Closing old issue.