我想在Facebook页面的墙上发布消息。我是该应用程序的管理员和此代码中使用的页面,我已经为我的应用程序提供了能够在我的页面上发布所需的权限,当我仅使用字段“message”时它可以工作,如下所示:
$message = array(
'message' => "Test2",<br>
);
$result = $fb->api('/411895472189524/feed','POST',$message);
上面的代码发布到我的页面墙上,帖子是“从”页面本身制作的,就像我会从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);
请参阅: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>';
}
答案 0 :(得分:2)
只需将答案作为答案发布。
作为网页发布时,您需要获得manage_pages
权限,然后通过access_token
API调用获取所需网页的/me/accounts
并使用该令牌进行/{page_id}/feed
POST呼叫。
Flames,原始海报,设法做到这一点,并在问题本身中编辑了他的解决方案。我只是把它贴在这里并制作社区维基
$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>';
}