通过Facebook API发布到页面 [英] Post to page via Facebook API

查看:119
本文介绍了通过Facebook API发布到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是FB应用程序和FB页面的管理员(但是这个页面在另一个帐户上)。如何通过FB应用程序使用FB API和PHP(以便能够使用CRON)来发布这个页面的墙壁?那可能吗?谢谢你的答案!

解决方案

是的,可以。



首先,使用页面访问令牌完成其代表页面的发布。



获取正常的令牌您的应用程式(可能是使用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆