How to check if user shared website (Facebook)? Is it possible?

前端 未结 2 1261
醉梦人生
醉梦人生 2021-02-15 23:45

I am currently writing a website and I need some help about facebook integration. I need a function (PHP or JS, both will help) that can check if a given user shared my website,

相关标签:
2条回答
  • 2021-02-15 23:56

    Using the JavaScript SDK, FB.ui() and the feed dialog, you can prompt your users to share a URL on Facebook. This dialog provides a callback function so that you can tell if the post was made successfully.
    Code sample lifted from the above link...

     var obj = {
        method: 'feed',
        link: 'https://developers.facebook.com/docs/reference/dialogs/',
        picture: 'http://fbrell.com/f8.jpg',
        name: 'Facebook Dialogs',
        caption: 'Reference Documentation',
        description: 'Using Dialogs to interact with users.'
    };
    
    function callback(response) {
        document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
    }
    
    FB.ui(obj, callback);
    

    This is the easiest way to accomplish what you need. It will not, however, allow you to test if someone shared your website's URL outside of your feed dialog.


    A more complex alternative not requiring the dialog callback could be accomplished by utilizing the read_stream permission. Once you have that permission, you can scan the users past posts to see if he has ever shared your website on his wall...

    Keep in mind that this will not work if the user shared your website on some one else's wall or on a page...

    0 讨论(0)
  • 2021-02-16 00:04

    First you have to load the Facebook SDK right after your tag:

    <div id="fb-root"></div>
      <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId                : "YOUR APP ID",
          status               : true, // check login status
          cookie               : true, // enable cookies to allow the server to access the session
          xfbml                : false,  // parse XFBML
          perms                : 'read_stream',
          access_token         : "USER ACCESS TOkEN",
          frictionlessRequests : true
        });
      };
    
      // Load the SDK Asynchronously
      (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/pt_BR/all.js";
        fjs.parentNode.insertBefore(js, fjs);
      }(document, 'script', 'facebook-jssdk'));
    
    </script>
    

    Then you can use the callback function to do what you want:

    <script type="text/javascript">
        function shareOnFacebook() {
        FB.ui(
          {
            method        : 'feed',
            display       : 'iframe',
            name          : 'name',
            link          : 'http://www.linktoshare.com',
            picture       : 'http://www.linktoshare.com/images/imagethumbnail.png',
            caption       : 'txt caption',
            description   : 'txt description',
            access_token  : 'user access token'
          },
          function(response) {
            if (response && response.post_id) {
    
              // HERE YOU CAN DO WHAT YOU NEED
              alert('OK! User has published on Facebook.');
    
            } else {
              //alert('Post was not published.');
            }
          }
        );
      }
    </script>
    

    Then you should use it like this:

    <a href="#" onclick="shareOnFacebook();">Share on facebook</a>
    
    0 讨论(0)
提交回复
热议问题