使用 Graph & 发布到 Facebook Wall PHP

发布于 2024-11-05 23:22:32 字数 1178 浏览 0 评论 0原文

我在使用开放图授权发布到访客 Facebook 墙上时遇到问题,并且似乎无法弄清楚我错在哪里。我正在使用 Github 上的 PHP/SDK 库。这是我的代码:

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
//session-based api call
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

//post to wall/stream function
if(isset($_POST['status'])) {
    try {
        $result = $facebook->api(
            '/me/feed',
            'post',
            array(
                'access_token' => 'ACCESS_TOKEN', 
                'message' => $_POST['status']
                )
            );
    } catch (FacebookApiException $e) {
        echo 'Error: ' . print_r($e, true);
    }
}
?>

<!-- HTML Form -->
<form name="" action="index.php" method="post">
    <input type="text" name="status" id="status" value="">
    <input type="submit" value="submit">
</form>

我不知道发生了什么,我梳理了十亿个教程、帖子、交流等,看起来我做的一切都是正确的。任何帮助将不胜感激。

I'm having trouble using the open graph authorization to publish to the visitors facebook wall, and can't seem to figure out where I'm wrong. I'm using the PHP/SDK Library from Github. Here's my code:

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
//session-based api call
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

//post to wall/stream function
if(isset($_POST['status'])) {
    try {
        $result = $facebook->api(
            '/me/feed',
            'post',
            array(
                'access_token' => 'ACCESS_TOKEN', 
                'message' => $_POST['status']
                )
            );
    } catch (FacebookApiException $e) {
        echo 'Error: ' . print_r($e, true);
    }
}
?>

<!-- HTML Form -->
<form name="" action="index.php" method="post">
    <input type="text" name="status" id="status" value="">
    <input type="submit" value="submit">
</form>

I don't know what's going on, I've combed through a billion tutorials, posts, exchanges, etc. and it seems like I'm doing everything right. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情绪少女 2024-11-12 23:22:32

在这里,您实际上并没有在发布之前检查用户是否已登录,而且我看不到您要求用户登录的位置。这是一个更好的方法:

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));

$session = $facebook->getSession();

if($session) {
    //post to wall/stream function
    if(isset($_POST['status'])) {
        try {
            $result = $facebook->api(
                '/me/feed',
                'post',
                array(
                    'message' => $_POST['status']
                    )
                );
        } catch (FacebookApiException $e) {
            echo 'Error: ' . print_r($e, true);
        }
    }
} else {
    $loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'publish_stream'
    ));
    header("Location: $loginUrl");
}
?>

<!-- HTML Form -->
<form name="" action="index.php" method="post">
    <input type="text" name="status" id="status" value="">
    <input type="submit" value="submit">
</form>

Here you are not actually checking if the user is logged in before posting and I can't see where you are asking the user to login. Here's a better approach:

<?php
require_once('facebook.php');
$facebook = new Facebook(array(
  'appId'  => 'APP_ID',
  'secret' => 'APP_SECRET',
  'cookie' => true,
));

$session = $facebook->getSession();

if($session) {
    //post to wall/stream function
    if(isset($_POST['status'])) {
        try {
            $result = $facebook->api(
                '/me/feed',
                'post',
                array(
                    'message' => $_POST['status']
                    )
                );
        } catch (FacebookApiException $e) {
            echo 'Error: ' . print_r($e, true);
        }
    }
} else {
    $loginUrl = $facebook->getLoginUrl(array(
        'req_perms' => 'publish_stream'
    ));
    header("Location: $loginUrl");
}
?>

<!-- HTML Form -->
<form name="" action="index.php" method="post">
    <input type="text" name="status" id="status" value="">
    <input type="submit" value="submit">
</form>
深白境迁sunset 2024-11-12 23:22:32

根据您编写的代码,假设您正在使用 Javascript SDK 生成 facebook cookie,这是 $facebook->getSession() 调用用来让用户登录的内容: 你的Javascript SDK设置正确吗?

此外,您还假设您使用的任何应用程序都已获得发布到用户墙上的适当权限。请参阅 http://developers.facebook.com/docs/authentication/permissions/有关所需权限的更多信息。您是否已获得用户请求的许可?

With the code you've written, it is assumed that you are using the Javascript SDK to produce the facebook cookie, which is what the $facebook->getSession() call uses to log the user in: do you have the Javascript SDK setup correctly?

Also, you are assuming that whatever app you are using has obtained the appropriate permission to post to the user's wall. See http://developers.facebook.com/docs/authentication/permissions/ for more information about the required permissions. Have you obtained the requested permission from the user?

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