使用 Graph & 发布到 Facebook Wall PHP
我在使用开放图授权发布到访客 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这里,您实际上并没有在发布之前检查用户是否已登录,而且我看不到您要求用户登录的位置。这是一个更好的方法:
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:
根据您编写的代码,假设您正在使用 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?