2011-07-11 111 views
0

我正在使用以下PHP代码发布到我的页面墙上。这是可以的,我可以作为管理员成功发布,但我想使用页面名称(页面帐户)发布到墙上。如何使用页面名称或页面标识进行发布。我试图找到很好的资源来解释Facebook的API函数,但我没有找到好的文档。使用PHP脚本作为页面帐户发布到Facebook页面的问题

<? 


require '../src/facebook.php'; 

$app_id = "XXX"; 
$app_secret = "YYY"; 

$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
'cookie' => true 
)); 

$user = $facebook->getUser(); 
//echo $user; 

if(($facebook->getUser())==0) 
{ 
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access'))}"); 
exit; 
} 
else { 
echo "i am connected"; 
} 

$attachment = array('message' => 'this is my message', 
       'name' => 'This is my demo Facebook application!', 
       'caption' => "Caption of the Post", 
       'link' => 'example.org', 
       'description' => 'this is a description', 
       'picture' => 'http://example.org/image.gif', 
       'actions' => array(array('name' => 'Get Search', 
            'link' => 'http://www.google.com')) 
       ); 
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789 
?> 
+1

“I am connected”讯息是否适合您? API是否返回错误消息? – GregSchoen

+0

是的。我可以张贴到墙上,这意味着连接是okay.no错误。我希望帖子作为页面名称而不是管理员名称 –

+0

当您对应用程序进行身份验证时,是否使用了页面所有者或管理员用户的凭据? (我假设他们是不同的?) – GregSchoen

回答

4

我找到了解决问题的办法。 要发布或执行任何任务作为页面,您需要访问令牌。 这是我最后的代码更新后,我想与大家分享,因为我发现有与得到正确的访问代码的网页困难了许多人。

<? 


require '../src/facebook.php'; 

$app_id = "XXX"; 
$app_secret = "YYY"; 

$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
'cookie' => true 
)); 

$user = $facebook->getUser(); 
//echo $user; 

if(($facebook->getUser())==0) 
{ 
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos,offline_access,manage_pages'))}"); 
exit; 
} 
else { 
$accounts_list = $facebook->api('/me/accounts'); 
echo "i am connected"; 
} 

//to get the page access token to post as a page 
foreach($accounts_list['data'] as $account){ 
     if($account['id'] == 123456789){  // my page id =123456789 
     $access_token = $account['access_token']; 
     echo "<p>Page Access Token: $access_token</p>"; 
     } 
    } 

//posting to the page wall 
$attachment = array('message' => 'this is my message', 
       'access_token' => $access_token, 
       'name' => 'This is my demo Facebook application!', 
       'caption' => "Caption of the Post", 
       'link' => 'example.org', 
       'description' => 'this is a description', 
       'picture' => 'http://example.org/image.gif', 
       'actions' => array(array('name' => 'Get Search', 
            'link' => 'http://www.google.com')) 
       ); 
$status = $facebook->api('/123456789/feed', 'POST', $attachment); // my page id =123456789 
var_dump($status); 
?> 
相关问题