2015-09-01 63 views
1

我有下面的代碼片段,它可以在Facebook上以我自己的用戶帳戶的形式發佈到Facebook頁面。Facebook SDK v5發佈爲牆上的頁面

FACEBOOK_*是在代碼庫中定義的。

// SDK Version 5.0 
$fb = new Facebook\Facebook([ 
    'app_id' => FACEBOOK_APP_ID, 
    'app_secret' => FACEBOOK_APP_SECRET, 
    'default_graph_version' => 'v2.4', 
]); 

// Returns a `Facebook\FacebookResponse` object 
$response = $fb->post('/'.FACEBOOK_PAGE_ID.'/feed', $postData, FACEBOOK_ACCESS_TOKEN); 

$postId = $response->getGraphNode(); 

現在我的問題是我怎樣才能讓它發佈爲實際頁面,而不是我的帳戶,這是該網頁的管理員。

我已經看了一下SDK文檔,我一直在繞圈,有很多v4的例子,但是因爲它已經被棄用了,我正在嘗試使用v5並且似乎無法想象它out,我發現的任何發佈歸屬或模擬鏈接都是SDK v5中的死鏈接。

從我所看到的,我需要打個電話到/ {用戶ID} /帳戶,以獲得從我的用戶頁面的訪問令牌,https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

但要獲得{user-id}我要查詢用戶,從SDK文檔類似下面的例子:

// Make sure to load the Facebook SDK for PHP via composer or manually 
use Facebook\FacebookRequest; 
use Facebook\GraphUser; 
use Facebook\FacebookRequestException; 

if($session) { 
try { 
    $user_profile = (new FacebookRequest(
     $session, 'GET', '/me' 
    ))->execute()->getGraphObject(GraphUser::className()); 
    echo "Name: " . $user_profile->getName(); 
} catch(FacebookRequestException $e) { 
    echo "Exception occured, code: " . $e->getCode(); 
    echo " with message: " . $e->getMessage(); 
} 

這裏的問題是,我不知道怎麼去,我需要得到該用戶數據的會議使我的訪問令牌允許我將訪問令牌傳遞到上面的代碼片段中,那就是如果我理解正確的話!

任何幫助非常感謝!

+0

你有沒有發現這個問題的解決方案?我用這個新的SDK得到了很多錯誤。 [在這裏發表的另一個問題](http://stackoverflow.com/questions/33523479/posting-on-a-facebook-page-with-php-sdk-v5) – Zl3n

+0

@ Zl3n,沒有抱歉,我沒有能力爲了得到它的工作,仍然試圖弄清楚,如果我知道了,我會回到這裏。 – llanato

+0

非常感謝,你讓我有一個我沒有想過的討論。有用 ! – Zl3n

回答

8

我工作的類,所以我適應我的代碼上面的例子。經測試和工作代碼。

在使用您使用的方法獲取用戶訪問令牌後(請參閱指南here),我們必須獲取長期訪問令牌。添加到您的代碼:

session_start(); 
$helper = $fb->getRedirectLoginHelper(); 

try { 
    $accessToken = $helper->getAccessToken(); 
} catch(Facebook\Exceptions\FacebookSDKException $e) { 
    // There was an error communicating with Graph 
    echo $e->getMessage(); 
    exit; 
} 

if (isset($accessToken)) { 

    $client = $fb->getOAuth2Client(); 

    try { 
     $accessToken = $client->getLongLivedAccessToken($accessToken); 
    } catch(Facebook\Exceptions\FacebookSDKException $e) { 
     echo $e->getMessage(); 
     exit; 
    } 


    $response = $fb->get('/me/accounts', (string) $accessToken); 

    foreach ($response->getDecodedBody() as $allPages) { 
     foreach ($allPages as $page) {    

      if (isset($page['id']) && $page['id'] == $pageId) { // Suppose you save it as this variable 
       $appAccessToken = (string) $page['access_token']; 
       break; 
      } 
     } 
    } 

    $response = $fb->post(
     '/'.$pageId.'/feed', 
     array(
      "message" => "Message", 
      "link" => "http://www.example.com", 
      "picture" => "http://www.example.net/images/example.png", 
      "name" => "Title", 
      "caption" => "www.example.com", 
      "description" => "Description example" 
     ), 
     $appAccessToken 
    ); 

    // Success 
    $postId = $response->getGraphNode(); 
    echo $postId; 

} elseif ($helper->getError()) { 
    var_dump($helper->getError()); 
    var_dump($helper->getErrorCode()); 
    var_dump($helper->getErrorReason()); 
    var_dump($helper->getErrorDescription()); 
    exit; 
} 

說明:你必須知道你是哪個網頁管理員:

$response = $fb->get('/me/accounts', (string) $accessToken); 

然後搜索表檢索the access token of the page我們感興趣的(我有選擇採用引用的頁面的ID)。

最後,只需運行由SDK提供的職位功能:

$response = $fb->post(
    '/'.$pageId.'/feed', 
    array(
     "message" => "Message", 
     "link" => "http://www.example.com", 
     "picture" => "http://www.example.net/images/example.png", 
     "name" => "Title", 
     "caption" => "www.example.com", 
     "description" => "Description example" 
    ), 
    $appAccessToken 
); 
+0

認爲我有它的工作原理,只要等待一旦測試完成就知道了,一旦我完成了測試並且一切正常,我們都會接受它作爲答案。 – llanato

+0

@llanato我很安靜,它完美的工作:)我正在努力創建這個包到現在 – Zl3n

+0

@ ZI3n,讓它最終與你的例子一起工作。 – llanato

相關問題