Hi,

I have built my site with zero knowledge in Drupal/PHP and Thanks to the Drupal community i had overcame a lot of difficulties.
Now i feel its my turn to give something back.

Following is a complete example written mostly in php of a scenario of how to check if user is connected, if not open a separate login window (including setting the required permissions).
After all set - publish a message on the user's wall.

I'll be happy to hear your comments:

<?php

/*
* Posting a message to a facebook user wall - This is the entry point
*/
function post_to_facebook_wall() {
	$facebook = facebook_client();

	$me = get_me_info($facebook);

	// login or logout url will be needed depending on current user state.
	if (!$me) {
		do_login($facebook);
	} 
	$feed = get_my_feed();
	publish_my_message($facebook);
}

/*
* Invoking the login window as a popup with permissions
*/
function do_login($facebook) {
    global $base_root; // Holds my site name
    $next_path = $base_root."/connect.php";
    $params = array (
        'fbconnect'=>1  ,
        'req_perms'=>'publish_stream,email',
        'canvas'=>0  ,
        'display'=>'popup',
        'next' => $next_path,
    );
    $loginUrl = $facebook->getLoginUrl($params);

    open_login_window($loginUrl);
}

/*
* Open login window
*/
function open_login_window($loginUrl) {
	?>
	<script type="text/javascript">
                var newwindow;
                width    = 500,
                height   = 270,
                left     = parseInt(screenX + ((outerWidth - width) / 2), 10),
                top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
                features = (
                  'width=' + width +
                  ',height=' + height +
                  ',left=' + left +
                  ',top=' + top
                );
         
                newwindow=window.open('<?=$loginUrl?>','Login by facebook',features);
	</script>
	<?php
}

/*
* Return array describes how the login window will look like
*/
function get_my_feed() {
	global $base_root;
	
	$msg = t('My Message!');
    $attachment = array(
         'name' => t('My Site Name'),
         'href' => url( $base_root, array('absolute' => TRUE)),
         'description' => t('The description of the attachment'),
         'media' => array (array(
                'type' => 'image',
                'src' => 'path/to/logo.jpg', 
                'href' => 'link/to/somwhere',
         )),
     );
	
	return array('message' => $msg, 
	             'attachment' => json_encode($attachment));
}

/*
* Return the user facebook info
*/
function get_me_info($facebook) {
	$me = null;
    $session = $facebook->getSession();
    // Session based API call.
    if ($session) {
        try {
            //drupal_set_message("Got Session");
            $uid = $facebook->getUser();
            $me = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
        }
    }
    else {
        //drupal_set_message("No Session");
    }
	
    return $me;
}

/*
* Publish the message on the wall
*/
function publish_my_message($facebook, $feed) {
	$session = $facebook->getSession();
	$me = $facebook->api('/me');
	publish_stream($facebook, $session, $feed);
	return $me;
}

/*
* a wrapper for the stream.publish facebook method
*/
function publish_stream($facebook, $session, $feed) {
	$userid = $facebook->getUser();
	$access_token = $session['access_token'];
	$facebook->api(array('method'=>'stream.publish',
	                     'message'=> $feed['message'],
	                     'attachment' => $feed['attachment'],
	                     'target_id'=>$userid, 
	                     'uid'=>$userid,
	                     'access_token'=>$access_token)
	);
}

Comments

chenop’s picture

Minor fixes was added...


/*
* Posting a message to a facebook user wall - This is the entry point
*/
function post_to_facebook_wall() {
	$facebook = facebook_client();

	$me = get_me_info($facebook);

	// login or logout url will be needed depending on current user state.
	if (!$me) {
		do_login($facebook);
	} 
	$feed = get_my_feed();
	publish_my_message($facebook, $feed);
}

/*
* Invoking the login window as a popup with permissions
*/
function do_login($facebook) {
    global $base_root; // Holds my site name
    $next_path = $base_root."/connect.php";
    $params = array (
        'fbconnect'=>1  ,
        'req_perms'=>'publish_stream,email',
        'canvas'=>0  ,
        'display'=>'popup',
        'next' => $next_path,
    );
    $loginUrl = $facebook->getLoginUrl($params);

    open_login_window($loginUrl);
}

/*
* Open login window
*/
function open_login_window($loginUrl) {
	
	<script type="text/javascript">
                var newwindow;
                width    = 500,
                height   = 270,
                left     = parseInt(screenX + ((outerWidth - width) / 2), 10),
                top      = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
                features = (
                  'width=' + width +
                  ',height=' + height +
                  ',left=' + left +
                  ',top=' + top
                );
         
                newwindow=window.open('<?=$loginUrl?>','Login by facebook',features);
	</script>
}

/*
* Return array describes how the login window will look like
*/
function get_my_feed() {
	global $base_root;
	
	$msg = t('My Message!');
    $attachment = array(
         'name' => t('My Site Name'),
         'href' => url( $base_root, array('absolute' => TRUE)),
         'description' => t('The description of the attachment'),
         'media' => array (array(
                'type' => 'image',
                'src' => 'path/to/logo.jpg', 
                'href' => 'link/to/somwhere',
         )),
     );
	
	return array('message' => $msg, 
	             'attachment' => json_encode($attachment));
}

/*
* Return the user facebook info
*/
function get_me_info($facebook) {
	$me = null;
    $session = $facebook->getSession();
    // Session based API call.
    if ($session) {
        try {
            //drupal_set_message("Got Session");
            $uid = $facebook->getUser();
            $me = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
        }
    }
    else {
        //drupal_set_message("No Session");
    }
	
    return $me;
}

/*
* Publish the message on the wall
*/
function publish_my_message($facebook, $feed) {
	$session = $facebook->getSession();
	$me = $facebook->api('/me');
	publish_stream($facebook, $session, $feed);
	return $me;
}

/*
* a wrapper for the stream.publish facebook method
*/
function publish_stream($facebook, $session, $feed) {
	$userid = $facebook->getUser();
	$access_token = $session['access_token'];
	$facebook->api(array('method'=>'stream.publish',
	                     'message'=> $feed['message'],
	                     'attachment' => $feed['attachment'],
	                     'target_id'=>$userid, 
	                     'uid'=>$userid,
	                     'access_token'=>$access_token)
	);
}
Anonymous’s picture

Subscribe

magahugu’s picture

Can I just put this PHP script into a rule that get's evaluated after the user logs in first time? Or where should I put this? I basically want to trigger the "Publish to Facebook" field just like after saving a node.

Thanks!
Urs