我想在页面上发布 - 通过我的网站。 .我没有找到任何可以帮助我的文档。谷歌搜索结果也没有给出我的答案。
function post_facebook($data=null){
$result = "";
require_once (ROOT. "/apps/configuration/models/ConfigurationItem.php");
require_once (ROOT . "/components/facebook/facebook.php");
$this->ConfigurationItem = new ConfigurationItem($this->getContext());
$row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_login');
$apiid=$row['value'];
$row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_pass');
$secret=$row['value'];
$facebook = new Facebook(array(
'appId' => $apiid,
'secret' => $secret,
'cookie' => true,
));
$session = $facebook->getSession();
print_r($session);
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
$message=$data['facebook_text'];
$attachment = array(
'message' => $data['facebook_text'],
'name' => $data['name'],
'link' => $this->getLinkToLatestNews(),
'description' => '',
);
if($data['thumb_file_tree_id'] !== NULL) $attachment = $_SERVER['HTTP_HOST']."media/file/image_by_id/".$data['thumb_file_tree_id']."/?w=400&h=500";
try {
$facebook->api('/162618213751448/feed/', 'post', $attachment);
$result = "Facebook: Sent";
} catch (FacebookApiException $e) {
$result = "Facebook: Failed";
error_log($e);
}
} else {
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
exit;
}
return $result;
}
错误的部分是:
$session = $facebook->getSession();
$me = null;
if ($session) {
(...)
} else {
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
exit;
}
我想让用户登录指定的 FB 帐户(带有页面),然后发送。应用程序设置只允许此帐户发帖,所以应该没问题......但不是。当我注销 FB 帐户时, session 仍然存在,但返回异常。怎么了?
最佳答案
如果您想在您是管理员的粉丝页面上发帖.. 执行下一步
您的应用程序至少需要具有此权限:
$this->loginUrl = $this->facebook->getLoginUrl( array( 'scope' => 'manage_pages,offline_access,publish_stream', 'redirect_uri' => $url, ) );
登录后:
//get pages or applications where you are administrator $accounts = $this->facebook->api('/me/accounts'); //page where i want to post $page_id = '227870047252297'; foreach($accounts['data'] as $account) { if($account['id'] == $page_id) { $token = $account['access_token']; } } $attachment = array( 'access_token' => $token, 'message' => 'mensaje', 'name' => 'titulo', 'link' => 'http://...', 'description' => 'xxxxx', 'picture'=> 'http://...', ); try{ $res = $this->facebook->api('/'.$page_id.'/feed','POST',$attachment); } catch (Exception $e){ echo $e->getMessage(); }
关于php - 使用 PHP SDK 在页面上发布 Facebook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4009352/