e-Zest members share technology ideas to foster digital transformation.

PHP Script for Sharing Message on Facebook

Written by Archana Mulye | May 8, 2014 2:33:12 PM

In the era of social networking, making your web presence popular is very useful especially through social network sites such as Facebook. The information available on the internet may not be precise and hence I have listed down a step-by-step guide for sharing messages on pages as well as user profile from your website using PHP Facebook SDK.

Step 1 –
Download PHP Facebook SDK by using following link:
https://github.com/facebook/facebook-php-sdk

Step 2 –
Become a Facebook app developer by using the following URL and create an app:
https://developers.facebook.com/ or https://developers.facebook.com/apps

Step 3 –

After creating the app followed through the steps, you will be redirected to the app settings page

Select “Website with Facebook Login” option and put your script URL from your website i.e. the website path for script, then click on ’Save Changes’.

You can integrate your script into the website for posting default messages, which will get shared on the user profile i.e. User Facebook timeline.

The code is as follows:


<?php

require '../src/facebook.php';   /* The path where facebook class file i.e. facebook.php resides */

 $appId = 'XXXXXXXXX';  /* Your app id which is created on facebook */

 $secret = 'XXXXXXXXXXXXXXXXX';  /* Your secret id for app which is created on facebook */

 $returnurl = 'http://localhost/facebook-php-sdk-master/facebook-php-sdk-master/src/fbposttouserwall.php';  /* The return url after script execution */

 $permissions = 'share_item, status_update';  /* permissions to be asked to user before posting */

 $fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));  /* Creating facebook class object to connect with app on facebook */

 $fbUserId = $fb->getUser();  /* getting the facebook user id */

 if($fbUserId){

        try{

                 $user_profile = $fb->api('/me','GET');

            $message = array(

                'message' => "Your message for post",

                                    'from' => $user_profile['name'],

                                    'name'=> $user_profile['name'],

                                    'description' => "Check Out new message"

            );

            $posturl = '/'.$ fbUserId.'/feed';

            $result = $fb->api($posturl,'POST',$message);      

            if($result){

                echo 'Successfully posted to Facebook Wall..';

            }

        }catch(FacebookApiException $e){

            echo $e->getMessage();

        }

 }else{

    $fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));

    echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';

 }

?>

If you want to post it on a particular page, you can also do that by including page id instead of user id in code. In the case of above a script, $posturl = '/'.$ fbUserId.'/feed'; instead of $ fbUserId you can put page id for e.g $posturl = '/XXXXXXXX/feed'; then the message will be posted on that particular page.