【发布时间】:2014-05-27 12:48:38
【问题描述】:
我正在使用 facebook 的 php SDK,我正在尝试在由登录用户管理/管理的页面上发帖。我已授予 publish_stream 和 manage_pages 权限。而且我希望帖子看起来像是由页面而不是管理员或登录用户制作的。我已经尝试了几种帮助,但没有人在工作。这是我现有代码的一部分:
require './php-fb-sdk/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook( array('appId' => 'xxxx', 'secret' => 'zzzz'));
// Get User ID
$user = $facebook -> getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook -> api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$params = array("scope" => array("manage_pages", "publish_stream"));
// Login or logout url will be needed depending on current user state.
if ($user) {
// Fetch the viewer's basic information
$basic = $facebook -> api('/me');
$permissions = $facebook -> api("/me/permissions");
if (array_key_exists('manage_pages', $permissions['data'][0]) && array_key_exists('publish_stream', $permissions['data'][0])) {
$admin_pages = $facebook -> api(array('method' => 'fql.query', 'query' => "SELECT page_id, type from page_admin WHERE uid=me() and type!='APPLICATION'"));
if (count($admin_pages) > 0) {
$post_info = $facebook -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
echo '<hr>The post info is: ' . print_r($post_info, true) . '<hr>';
} else {
echo '<hr> You are not admin of any fb fan page<hr>';
}
//print_r($admin_pages);
} else {
// We don't have the permission
// Alert the user or ask for the permission!
header("Location: " . $facebook -> getLoginUrl(array("scope" => "manage_pages,publish_stream")));
}
$logoutUrl = $facebook -> getLogoutUrl();
} else {
//$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook -> getLoginUrl($params);
$statusUrl = $loginUrl;
}
使用上面的代码我可以在页面上发布,但看起来它是由用户制作的。
但是,如果我使用 facebook JS SDK,我会看到帖子看起来像是由页面制作的。
var data=
{
caption: 'My Caption',
message: 'My Message'
}
FB.api('/' + pageId + '/feed', 'POST', data, onPostToWallCompleted);
}
任何帮助或建议将不胜感激。
【问题讨论】:
标签: php facebook facebook-graph-api facebook-php-sdk