Irene Ling
Irene Ling

Reputation: 1971

update database when a user shared a link to facebook

Is it possible to update database once a user shared a link in my website?

Such as,some points will be awarded to their account if they shared the link.

And how can I ensure that they really share the link to facebook but not just click share then close the popup window...

I'm not familiar with facebook , and tried google it just now but still didn't find the answer...

Thank you.

Edit:

   <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" 
    type="text/javascript">
   </script>

   <a name="fb_share" type="button" share_url="script2.php?id=XXX"> 

Upvotes: 3

Views: 3745

Answers (4)

bool.dev
bool.dev

Reputation: 17478

Update(Aug 21, 2013): Facebook's api has changed since the time this was originally posted. The way to track likes is still the same (read original answer below), but the status of the share button has changed.

The deprecation message/info. is no longer visible anywhere in the docs, but it can be seen in this bug. The share button still seems to be usable according to this doc. But there is no direct way from facebook to track a share. I'm not aware of any hacks to track shares either.


Original answer:

From Facebook docs at this link.

The Share button has been deprecated in favor of the Like button, and will no longer be supported. Please use the Like button whenever possible to drive maximum traffic to your apps.

So I'll tell you how to do it for a like button:

First use the Javascript sdk

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID
      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
    // you can subscribe to events here
    // edge.create event is fired only when the user likes (which means that the wall post has already happened, when the like button was clicked) 
    FB.Event.subscribe('edge.create',
   function(response) {
    alert('You liked the URL: ' + response);
            // you can do some ajax call to your backend and update your database
   }
); 
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));
  </script>

Somewhere in your page include the like button

<fb:like href="http://example.com/fblike/" 
     send="true" width="450" show_faces="false"
     >
</fb:like>

You can also change the word that is displayed in the like button, from like to recommend, you should try and auto-generate your like button code from this link.

Upvotes: 6

Zul
Zul

Reputation: 3608

Yes, it is possible.

Example Link:

<a href="?share=YOUR+URL">SHARE TO FACEBOOK</a>

Example code to proses

if(isset($_GET['share']))
{
    if(mysql_query("insert into table X() value()"))
    {
        //open window, you can use header('location:..') or meta redirect or another way, in this sample i'm using javascript window.open;
        echo '<script>window.open("http://www.facebook.com/sharer.php?u='.urlencode($_GET['share']).'","share","width=600,height=400");</script>';
    }
}

To implementing facebook "Like" button, this link maybe can help you

Upvotes: 3

coderek
coderek

Reputation: 1870

I guess you are looking for this http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/ You need to subscribe this event.

Upvotes: 6

brains_at_work
brains_at_work

Reputation: 251

You do not need facebook, you could just have a Javascript AJAX call executed when someone clicks the Like button on your page. The call writes to your database. Just bin a click event to the button.

Upvotes: 1

Related Questions