使用 PHP 更新 Facebook 墙

发布于 2024-11-09 17:13:50 字数 1552 浏览 0 评论 0原文

我有一个博客系统,我希望用户能够在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不寐倦长更 2024-11-16 17:13:50

为了发布到某人的墙,您的应用程序必须具有该用户的墙发布权限。为了获得这些权限,用户必须登录 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/

小草泠泠 2024-11-16 17:13:50

另外,如果您想在用户未登录时发布信息,则需要“offline_access”。

Also, you need "offline_access" if you want to post when's the user is not logged in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文