简体   繁体   中英

Share button/post to wall - Facebook API?

I don't want to use the FB like button and apparently "share" has been deprecated. What I am trying to do is have users click "share"/"post to wall" from my website, and it then places a post on their newsfeed/profile with information on my website/url.

I can't seem to find any code around that will do this- does anyone have an example?

And do they have to connect first? Or can it check if they're logged in, if not, login and it shares automatically?

Thanks!

This is possible in two ways:

  • You can use the facebook Javascript SDK if you have an app:
    FB.ui({
        method: 'feed',  
        link: 'absolute url',
        name: 'testtitle',
        caption: 'testcaption',
        description: 'testdescription',
        picture: 'absolute picurl',
        message: ''
    });

Note that "message" MUST be empty, you can also just remove it.

  • Without an app (no user can block the app and never get anything from the app anymore, but only possible with popup): open a popup window with Javascript for the facebook sharer:

     http://www.facebook.com/sharer.php?u=<url to share>&t=<title of content> 

    Note that everything needs to be urlencoded . Of course you can also just use it as a link. And don't forget the og tags in this case.

Edit: Please be aware that "auto sharing" is not allowed on facebook. you have to present the user what you want to share in his name and he has to be able to accept it and add his personal message. would only be possible with an app and an authorized user anyway.

Btw, both methods explained here work without user login/authorization.

Edit2: There is also a "share" method with FB.ui now, to post a link or use Open Graph Actions/Objects.

If you have a dynamic web site as I do, you may strongly want my code.

Note 1: You can't do that if you don't have an app! If you don't have an app you can simply go to https://developers.facebook.com/apps and create one.

Note 2: Read my code comments!

Code:

<? 
$redirect       = "http://www.SITE.com/thanks.html"; //After sharing, you redirect your visitor to thanks.html or just to the home page. Note that the URL given is the URL you set for your app! 
$link           = curPageURL(); //URL to the shared page (I will give you the function curPageURL() later).
$title       = Title(); //Title of the shared page (Note If you don't have a dynamic website you can simply ignore the PHP part)
$descriptionTag = Description(); //Description of the shared page
$pic             = Img(); //Image of the post or the logo of your website
echo "<script> 
        FB.init({appId: \"YOU_APP_ID_HERE\", status: true, cookie: true});
        function postToFeed() {
          // calling the API ...
          var obj = {
            method: 'feed',
            redirect_uri: '".$redirect."',
            link: '".$link."', 
            picture: '".$pic."',
            name: '".$title."',
            caption: '".$descriptionTag."',
            description: 'You_May_Want_To_Say_Something_About_Your_Web_Site_Here!'
          };
          function callback(response) {
            document.getElementById('msg').innerHTML = \"Post ID: \" + response['post_id'];
          }
          FB.ui(obj, callback);
        }
     </script>"; ?>
<a href="#" onclick='postToFeed(); return false;'>Share To Facebook</a>

Note: Don't forget to set YOUR APP ID in the code!

You need to use the function curPageURL() in order to share the current PHP page!

Code:

<?
function curPageURL() {
 $pageURL = 'http';
 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
?>

Don't forget to declare the function curPageURL() at the beginning of the code I'm giving to you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM