Search code examples
phpfacebookfacebook-javascript-sdksocial-networkingfacebook-social-plugins

One off 'share' on facebook button


I am building a simple online game which I would like users to be prompted to share their score on facebook when they complete the game. I don't really want to complicate the UI too much and ideally would like it to work similar to twitter's tweet button and have a predefined (editable) message like:

"I have just scored 560 on mygame.com"

I would like this to get posted onto the user's facebook wall.

What is the easiest option for code and for user? I'm working in php, mysql, jquery, javascript, ajax


Solution

  • Suggest you use the Javascript SDK: https://developers.facebook.com/docs/reference/javascript/

    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'YOUR_APP_ID', // App ID - get one at https://developers.facebook.com/apps/
          channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });
    
        // Additional initialization code here - Change fields below to match what you want to share and wrap it inside a function that you trigger on click
        FB.ui({
          method: 'feed',
          name: 'Facebook Dialogs',
          link: 'https://developers.facebook.com/docs/reference/dialogs/',
          picture: 'http://fbrell.com/f8.jpg',
          caption: 'Reference Documentation',
          description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
        },function(response) {
          if (response && response.post_id) {
            alert('Post was published.');
          } else {
            alert('Post was not published.');
          }
        });
      };
    
      // Load the SDK Asynchronously
      (function(d){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         ref.parentNode.insertBefore(js, ref);
       }(document));
    </script>
    

    The channel file can be just:

    <script src="//connect.facebook.net/en_US/all.js"></script>