RSS

Tag Archives: publish

Publishing streams on friends’ walls in Facebook

One of the frequent functionalities of the Facebook applications is the possibility of posting streams on friends’ walls. This is accomplished very easy thanks to the PHP SDK 3.0 framework of Facebook.

This article expects that the SDK is installed and included.

The first thing to do is create the application instance.

$facebook = new Facebook(array(
   'appId'  => FB_APP_ID,
   'secret' => FB_APP_SECRET,
   'cookie' => true,
));

You will have to request extended permissions for posting on the wall by setting 'scope' => 'publish_stream' in the authorization link.

If you don’t know how to do it I suggest to read the “Authorizing a Facebook application” article first.

Using FQL the friends list of the user:

try{
   $fql = "select name,username,pic_square,uid from user where uid in (select uid1 from friend
           where uid2=" . $user.") order by name";
   $param  =   array(
      'method'    => 'fql.query',
      'query'     => $fql,
      'callback'  => ''
      );
   $friend_list   =   $facebook->api($param);
}
catch(Exception $o){
   error_log ($o);
}

Once we have the list we’re only a loop away from sending the stream to the friends:

$attachment =  array
  (
  'access_token'=>$facebook->getAccessToken(),
  'message' => 'The message from the user - probably a submitted <textarea>',
  'name' => 'Name of the link',
  'caption' => 'Caption',
  'link' => 'the url to your application or whatever you wish',
  'description' => 'our description',
  'picture' => 'url of the image preview - required for flash',
  'type' => 'flash',
  'source' => 'the url to the flash file',
  );

foreach ($friend_list as $friend){
   try {
   $statusUpdate = $facebook->api('/'.$friend.'/feed', 'post', $attachment);
   }
   catch (FacebookApiException $e) {
      error_log($e);
   }
}
The published stream

Example of publishing stream on a friend's wall

It’s worth a notice that there are several types of media which you can send. They are specified in the ‘type’ parameter and can include link, video, music.

For a full reference of all the POST parameters check the official documentation which might not be that great but’s still better than nothing. 🙂

Happy streaming!

 
Leave a comment

Posted by on 17 Jun 2011 in Facebook

 

Tags: , , , , ,