2011-09-09 52 views
14

我不想使用FB的按钮,显然“共享”已被弃用。 我想要做的是让用户点击我的网站上的“share”/“post to wall”,然后在他们的newsfeed /个人资料上发布一篇文章,其中包含我网站/网址上的信息。分享按钮/贴到墙上 - Facebook API?

我似乎无法找到任何代码周围,会做这个 - 没有任何人有一个例子吗?

,做他们必须先连接?或者可以检查他们是否已登录,如果没有,登录并自动共享?

谢谢!

回答

20

这是可能有两种方式:

  • 您可以使用Facebook的Javascript SDK,如果你有一个应用程序:
FB.ui({ 
     method: 'feed', 
     link: 'absolute url', 
     name: 'testtitle', 
     caption: 'testcaption', 
     description: 'testdescription', 
     picture: 'absolute picurl', 
     message: '' 
    }); 

注意, “消息” 必须是空的,你可以也只是删除它。

  • 没有一个应用程序(没有用户可以阻止应用程序,并从来没有从应用程序什么了,但只能使用弹出式): 打开一个弹出窗口,使用Javascript Facebook的分享者:

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

    请注意,一切都需要urlencoded。当然你也可以把它当作链接来使用。在这种情况下不要忘记og标签。

编辑:请注意, “汽车共享” 是不允许在Facebook上。你必须向用户展示你想分享他的名字,他必须能够接受并添加他的个人信息。无论如何只有应用程序和授权用户才有可能。

顺便说一句,这两种方法在这里解释,而无需用户登录/授权工作。

编辑2:现在还有一个“共享”方法FB.ui来发布链接或使用Open Graph Actions/Objects。

-2

如果你有一个动态的网站,像我一样,你可以强烈地想我的代码。

注意1:如果您没有应用程序,则不能这样做!如果你没有 的应用程序,你可以简单地去https://developers.facebook.com/apps和 创建一个。

注2:阅读我的代码注释!

代码:

<? 
$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> 

注: 不要忘记设置你的App ID代码!

您需要使用函数curPageURL()才能共享当前的PHP页面!

代码:

<? 
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; 
} 
?> 

不要忘了声明函数curPageURL()在我给你的代码的开始!