问题描述
我想在页面上发布 - 通过我的网站。 。我没有找到任何可以帮助我的文档。也没有一个谷歌的结果给了mi答案。
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);
退出;
}
return $ result;
}
错误的部分是:
$ session = $ facebook-> getSession();
$ me = null;
if($ session){
(...)
} else {
$ login_url = $ facebook-> getLoginUrl();
header(Location:。$ login_url);
退出;
}
我想允许用户在指定的FB帐户(此页面)上登录,然后发送。应用设置只允许这个帐户发布,所以应该是可以的...但不是。当我注销FB帐号时,会话仍然存在,但返回异常。怎么了?
如果你想在一个粉丝页面上发布你是管理员..做下一个
-
您的应用程序需要最小权限:
$ this-> loginUrl = $ this-> facebook-> getLoginUrl(
array(
'scope'=>'manage_pages,offline_access,publish_stream',
'redirect_uri'=> $ url,
)
);
-
登录后:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
//我要发布的页面
$ 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:// ...',
);
尝试{
$ res = $ this-> facebook-> api('/'.$ page_id。'/ feed','POST',$ attachment );
} catch(异常$ e){
echo $ e-> getMessage();
}
I'd like to post on page - throught my site. . I didn't find anything that could help me in documentation. Also none of google results gave mi answer.
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;
}
The wrong part is:
$session = $facebook->getSession();
$me = null;
if ($session) {
(...)
} else {
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
exit;
}
I want to allow user to login on specified FB account (this with page), and then send. App settings allow only this account to post, so it should be ok... But isn't. When I log out FB account, session still exists, but returns exception. What's wrong?
if you want to post on a fan page where you are adminstrator.. do the next
Your application needs to have this permission minimum:
$this->loginUrl = $this->facebook->getLoginUrl( array( 'scope' => 'manage_pages,offline_access,publish_stream', 'redirect_uri' => $url, ) );
After you are logged into:
//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(); }
这篇关于Facebook在PHP页面上发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!