<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">

PHP Script for Sharing Message on Facebook

Archana Mulye May 08, 2014

Technology

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

Create new Facebook app

Step 3 –

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

Facebook App setting

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.

 

Similar Blog

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.