通过Facebook API发布到页面 [英] Post to page via Facebook API
问题描述
是的,可以。
首先,使用页面访问令牌完成其代表页面的发布。
获取正常的令牌您的应用程式(可能是使用 Graph API Explorer 直接从右上角选择您的应用程式菜单),权限: manage_pages
,然后按照我在此提到的步骤: https://stackoverflow.com/a/18322405/1343690 - 这将让您获得永不过期的页面访问令牌。
将其保存在某处,并在发布时与您的cron-job一起使用。发布代码 -
$ url ='https://graph.facebook.com/{page-id}/feed' ;
$ attachment = array(
'access_token'=> $ page_access_token,
'message'=>'{your-message}'
);
$ result = PostUsingCurl($ url,$ attachment);
$ result = json_decode($ result,TRUE);
if(isset($ result ['error'])){
echoError:。$ result ['error'] ['message']。< br />;
}
else {
echoFeed发布成功!< br />;
}
函数PostUsingCurl($ url,$ attachment)
{
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,$ url);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ ch,CURLOPT_SSL_VERIFYHOST,2);
curl_setopt($ ch,CURLOPT_POST,true);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ attachment);
curl_setopt($ ch,CURLOPT_HEADER,0);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
$ result = curl_exec($ ch);
curl_close($ ch);
return $ result;
}
I'm the admin of FB app and FB page (but this page on a separate account). How do I can post something to this page's wall via this FB app using FB API and PHP (in order to be able to do it with CRON)? Is that possible? Thank you in advance for your answers!
Yes it is possible.
First of all, to post on a page on its behalf is done using the page access token.
Get a normal token from your app (may be using Graph API Explorer directly by selecting your app from top-right drop down menu) with the permission: manage_pages
, then follow the steps I've mentioned here: https://stackoverflow.com/a/18322405/1343690 - this will get you a never-expiring page access token.
Save it somewhere and use with your cron-job while posting. Code for posting-
$url = 'https://graph.facebook.com/{page-id}/feed';
$attachment = array(
'access_token' => $page_access_token,
'message' => '{your-message}'
);
$result = PostUsingCurl($url, $attachment);
$result = json_decode($result, TRUE);
if( isset($result['error']) ) {
echo "Error: ".$result['error']['message']."<br/>";
}
else{
echo "Feed posted successfully!<br/>";
}
function PostUsingCurl($url, $attachment)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
这篇关于通过Facebook API发布到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!