FacebookPHPSDK:我如何代表页面在页面上发布
我正在使用facebook的PHPSDK,并试图在一个由登录用户管理的页面上发布文章。我已授予发布流和管理页面权限。我希望这篇文章看起来应该像是由页面而不是管理员或登录用户制作的。我试过几次帮助,但都没人帮上忙。以下是我现有代码的一部分:FacebookPHPSDK:我如何代表页面在页面上发布,php,facebook,facebook-graph-api,facebook-php-sdk,Php,Facebook,Facebook Graph Api,Facebook Php Sdk,我正在使用facebook的PHPSDK,并试图在一个由登录用户管理的页面上发布文章。我已授予发布流和管理页面权限。我希望这篇文章看起来应该像是由页面而不是管理员或登录用户制作的。我试过几次帮助,但都没人帮上忙。以下是我现有代码的一部分: require './php-fb-sdk/facebook.php'; // Create our Application instance (replace this with your appId and secret). $facebook = ne
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 -> api('/' . $admin_pages[0]["page_id"] . '/feed', 'post', array("caption" => "From web", "message" => "This is from my web at: " . time()));
正在使用默认(用户)访问令牌
要获取页面访问令牌,请在发布帖子之前进行此调用:
\GET /{page-id}?fields=access_token
这将在结果中获得一个页面访问令牌,然后简单地使用它进行发布提要调用,如下所示-
$facebook -> api(
'/' . $admin_pages[0]["page_id"] . '/feed',
'post',
array(
"caption" => "From web",
"message" => "This is from my web at: " . time(),
"access_token" => '{page_access_token}'
)
);
(如果需要,您还可以获取页面的永不过期令牌,请在此处检查如何:)
$facebook -> api(
'/' . $admin_pages[0]["page_id"] . '/feed',
'post',
array(
"caption" => "From web",
"message" => "This is from my web at: " . time(),
"access_token" => '{page_access_token}'
)
);