Fla*_*mes 11 php facebook facebook-graph-api facebook-php-sdk
我想在Facebook页面的墙上发布消息.我是该应用程序的管理员和此代码中使用的页面,我已经为我的应用程序提供了能够在我的页面上发布所需的权限,当我仅使用字段"message"时它可以工作,如下所示:
$message = array(
'message' => "Test2",<br>
);
$result = $fb->api('/411895472189524/feed','POST',$message);
Run Code Online (Sandbox Code Playgroud)
上面的代码发布到我的页面墙上,帖子是"从"页面本身制作的,就像我会从Facebook手动完成一样.这很有效.
但是,当我尝试添加更多字段,如"链接"或"图片"或"描述"时,帖子会出现在"其他人在TEST Jojo页面上的帖子"中,而帖子现在是通过我的个人帐户(Joelle Landrie)制作的.来自页面本身.见下面的代码.
$message = array(
'message' => "Test2",
'picture' => "http://www.cleanpopo.com/uploads/1/3/1/5/13154615/245431315.jpg",
'description' => "This is a test description",
'link' => "google.com"
);
$result = $fb->api('/411895472189524/feed','POST',$message);
Run Code Online (Sandbox Code Playgroud)
请参阅:https://www.facebook.com/pages/TEST-Jojo-Page/411895472189524
本link场似乎是造成问题,我可以让我的页面上的成功后使用message,picture并description现场.只有这对我没用,我需要我的帖子才能有链接.
感谢Shadowfax,他问我是否使用了"page_access_token".我不是.我开始在网上查看如何获取此令牌,将其添加到我的代码中,现在它工作得很好!!
$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = 'http://www.yoursite.com';
$permissions = 'manage_pages, publish_stream, offline_access';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
$page_id = "YOUR PAGE ID";
$page_access_token = "";
$result = $fb->api("/me/accounts");
// loop trough all your pages and find the right one
if( !empty($result['data']) )
{
foreach($result["data"] as $page)
{
if($page["id"] == $page_id)
{
$page_access_token = $page["access_token"];
break;
}
}
}
else
{
echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
}
// set the facebook active facebook access token as the one we just fetch
$fb->setAccessToken($page_access_token);
// Now try to post on page's wall
try{
$message = array(
'message' => "YOUR MESSAGE",
'picture' => "YOUR PICTURE",
'description' => "YOUR DESCRIPTION",
'link' => "YOUR LINK"
);
$result = $fb->api('/'.$page_id.'/feed','POST',$message);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
}
Run Code Online (Sandbox Code Playgroud)
只是将答案发布为答案。
作为页面发布时,您需要获得许可,然后通过API 调用manage_pages获取所需页面并使用该令牌进行POST 调用。access_token/me/accounts/{page_id}/feed
最初的发布者 Flames 设法做到了这一点,并发布了他在问题本身中编辑的解决方案。我只是将其粘贴到此处并使其成为社区 Wiki
$appId = 'YOUR APP ID';
$secret = 'YOUR SECRET';
$returnurl = 'http://www.yoursite.com';
$permissions = 'manage_pages, publish_stream, offline_access';
$fb = new Facebook(array('appId'=>$appId, 'secret'=>$secret));
$fbuser = $fb->getUser();
if($fbuser){
$page_id = "YOUR PAGE ID";
$page_access_token = "";
$result = $fb->api("/me/accounts");
// loop trough all your pages and find the right one
if( !empty($result['data']) )
{
foreach($result["data"] as $page)
{
if($page["id"] == $page_id)
{
$page_access_token = $page["access_token"];
break;
}
}
}
else
{
echo "AN ERROR OCCURED: could not get the access_token. Please verify the page ID ".$page_id." exists.";
}
// set the facebook active facebook access token as the one we just fetch
$fb->setAccessToken($page_access_token);
// Now try to post on page's wall
try{
$message = array(
'message' => "YOUR MESSAGE",
'picture' => "YOUR PICTURE",
'description' => "YOUR DESCRIPTION",
'link' => "YOUR LINK"
);
$result = $fb->api('/'.$page_id.'/feed','POST',$message);
if($result){
echo 'Successfully posted to Facebook Wall...';
}
}catch(FacebookApiException $e){
echo $e->getMessage();
}
}else{
$fbloginurl = $fb->getLoginUrl(array('redirect-uri'=>$returnurl, 'scope'=>$permissions));
echo '<a href="'.$fbloginurl.'">Login with Facebook</a>';
}
Run Code Online (Sandbox Code Playgroud)