简体   繁体   English

使用Graph和PHP发布到Facebook Wall

[英]Publish to Facebook Wall with Graph & PHP

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. 我在使用开放图授权将其发布到访问者Facebook墙上时遇到了麻烦,并且似乎无法弄清楚我哪里错了。 I'm using the PHP/SDK Library from Github. 我正在使用Github的PHP / SDK库。 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. 任何帮助将不胜感激。

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>

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? 使用您编写的代码,假定您使用的是Javascript SDK来生成facebook cookie,这是$facebook->getSession()调用用来登录用户的内容:您是否具有Javascript SDK设置正确吗?

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. 有关所需权限的更多信息,请参见http://developers.facebook.com/docs/authentication/permissions/ Have you obtained the requested permission from the user? 您是否已获得用户请求的许可?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM