2011-07-11 110 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); 
?> 
相關問題