使用 PHP 更新 Facebook 墙
我有一个博客系统,我希望用户能够在 Facebook 墙上发帖,而无需登录 Facebook。我希望通过 Facebook 提供的 PHP SDK 来实现这一点。
这是我授予我创建的 Facebook 应用程序的权限:
https://www.facebook.com/dialog/oauth?client_id=xxxxxxxx&redirect_uri=http://www.mydomain.com/facebook/index/&scope=offline_access,publish_stream,read_stream,manage_pages
只是为了澄清权限:offline_access、publish_stream、read_stream、manage_pages
以下代码仅在用户登录时才有效。否则我会收到此错误:
FacebookApiException [ 0 ] :必须使用活动访问令牌来查询有关当前用户的信息。
public function facebook()
{
require_once('facebook/php-sdk-3.0.0/src/facebook');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://www.google.com',
'description' => 'Test of application PHP',
'picture' => 'http://www.lalibre.be/img/logoLaLibre.gif',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'http://www.google.com')
)
);
$result = $facebook->api('/me/feed/','post',$attachment);
print_r($facebook->getAccessToken());
}
I have a blog system where I want the user to be able to post on there Facebook wall without them having to login to Facebook. I would like it to happen through the PHP SDK delivered by Facebook.
This is the permissions I have given the Facebook application i created:
https://www.facebook.com/dialog/oauth?client_id=xxxxxxxx&redirect_uri=http://www.mydomain.com/facebook/index/&scope=offline_access,publish_stream,read_stream,manage_pages
Just to clarify the permissions: offline_access,publish_stream,read_stream,manage_pages
Below codes only works if the user is logged in. else I get this error:
FacebookApiException [ 0 ]: An active access token must be used to query information about the current user.
public function facebook()
{
require_once('facebook/php-sdk-3.0.0/src/facebook');
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$attachment = array(
'access_token' => $facebook->getAccessToken(),
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://www.google.com',
'description' => 'Test of application PHP',
'picture' => 'http://www.lalibre.be/img/logoLaLibre.gif',
'actions' => array(array(
'name' => 'Get Search',
'link' => 'http://www.google.com')
)
);
$result = $facebook->api('/me/feed/','post',$attachment);
print_r($facebook->getAccessToken());
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了发布到某人的墙,您的应用程序必须具有该用户的墙发布权限。为了获得这些权限,用户必须登录 Facebook 并授予您访问权限。
欲了解更多信息:http://developers.facebook.com/docs/authentication/
In order to post to someone's wall your application must have wall post permissions for that user. In order to get those permissions the user must login to facebook and grant you access.
For more info: http://developers.facebook.com/docs/authentication/
另外,如果您想在用户未登录时发布信息,则需要“offline_access”。
Also, you need "offline_access" if you want to post when's the user is not logged in.