使用脚本发布到Facebook墙,自动登录

时间:2011-05-02 20:36:56

标签: facebook facebook-graph-api

使用PHP脚本(和Graph API)登录后,我可以发布到我的Facebook墙页面。我的主网站上有一个新闻页面,并希望将新闻项目发布到Facebook,因为它们被发布到我的网站上(我猜这是因为有多少人使用了API)。

对于FB应用程序,我已将offline_access和publish_stream授予我的FB帐户。

如果我关闭我的FB会话并尝试从我的新闻页面发帖,Facebook会显示一个登录页面。我已经授予访问权限,所以我不知道还有什么我想念的。

1 个答案:

答案 0 :(得分:0)

如果我理解你的问题,我想我刚刚解决了同样的问题。我无法使用图形API完全解决它,但图形api和传统rest api的组合起了作用。一旦您授予应用程序发布到Facebook页面墙的权限,以下代码应该按照您的要求执行:

$url = "https://graph.facebook.com/oauth/access_token";
$client_id = "XXXXXXXXXXXXXX";
$client_secret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$postString = "client_id=$client_id&client_secret=$client_secret&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
$token = curl_exec($curl);
curl_close($curl);

$message = rawurlencode($description.'. Notes:'.$notes);
$url = "https://api.facebook.com/method/stream.publish";
$postString = "message=$message&uid=XXXXXXXXXXXXX&$token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
$response = curl_exec($curl);
curl_close($curl);

很少有值得注意的事情 1.其余的api $ postString中的uid是你的facebook PAGE的uid。您不需要target_id,因为当uid是页面时,它只能发布到自身 2.您的$ message必须是rawurlencoded,因为这是其余api接受的格式 3.这将作为您的APP发布到您的页面,而不是发布到您网站的用户。要做到这一点,您需要拥有所有用户的publish_stream权限。

希望这有帮助。