2010-10-29 57 views
13

這是PHP的最後一次嘗試,如果失敗,我會嘗試使用JS。所以我的目標是張貼FB頁面上的「網頁名稱」通過PHP:這是我想要得到Facebook Graph API PHP SDK在頁面上張貼

View 1

但我得到如下所示照片。此外,它僅可用於此配置文件(不適用於喜歡/ etc的朋友/ ppl)。

View 2

這是我當前的代碼

function post_facebook($data=null, $redir = null){ 
     $result = ""; 
     require_once (ROOT. "/apps/configuration/models/ConfigurationItem.php"); 
     require_once (ROOT . "/components/facebook/facebook.php"); 

     $this->ConfigurationItem = new ConfigurationItem($this->getContext()); 

     $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_login'); 
     $apiid=$row['value']; <= Correct apiid 

     $row=$this->ConfigurationItem->findByCatKeyItemKey('system','facebook_pass'); 
     $secret=$row['value']; <= Correct secret key 

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

     $session = $facebook->getSession(); 
     $me = null; 
     if ($session) { 
      try { 
       $uid = $facebook->getUser(); 
       $me = $facebook->api('/me'); 
      } catch (FacebookApiException $e) { 
       error_log($e); 
      } 
      $message=$data['facebook_text']; 
      $attachment = array(
       'message' => $data['facebook_text'], 
       'name' => $data['name'], 
       'link' => $this->getLinkToLatestNews(), 
       'description' => '', 
      ); 

      try { 
       $facebook->api('/PAGE ID/feed/', 'post', $attachment); 
       $result = "Facebook: Sent"; 
      } catch (FacebookApiException $e) { 
       $result = "Facebook: Failed"; 
       error_log($e); 
      } 
     } else { 
      $login_url = $facebook->getLoginUrl(); 
      header("Location: ".$login_url); 
      exit; 
     } 

     echo $result; 
     exit; 
     //return $result; 

    } 

我做錯了什麼?我在API文檔/頂級谷歌搜索結果中找不到任何東西,只能用於JS。感謝幫助!

回答

20

您需要確保您要求用戶的'manage_pages'權限。一旦你得到了,你可以做$facebook->api('/me/accounts'),你會收到一個令牌回(連同頁面信息),你可以用它在頁面上發佈爲頁面。

+0

的關鍵,解決這個問題是越來越訪問令牌 - 我錯過了前面的部分。我欠你! – Misiur 2010-10-29 20:28:12

+0

嗨,你是否能夠發佈一個鏈接與圖像(如頁作爲海報)? – user3329793 2014-02-23 01:28:08

0

我在這一天的大部分時間裏掙扎着,然後發現不使用setAccessToken(page_access_token)是阻止它爲我工作的唯一東西。我在18個月前的一篇文章中發現了這個帖子。我在這裏把我的解決方案,任何人誰在未來有這樣一個問題:

protected $scope = "email,publish_stream,manage_pages"; 

    $url = "{$api_url}/{$fbusername}/accounts?access_token=".$access_token; 
    $response = json_decode(file_get_contents($url)); 
    foreach($response->data as $data) { 
     try 
     { 
      $res = $this->SDK->setAccessToken($data->access_token); 
      $res = $this->SDK->api(
       "{$data->id}/feed", 
       "POST", 
       array('link' => 'www.example.com', 
         'message' => 'This is a test message from php',) 
      ); 
      log::debug(__FUNCTION__, print_r($res,true)); 
     } 
     catch (Exception $e) 
     { 
      log::debug(__FUNCTION__, $e->getType().": ".$e->getMessage()); 
     } 

    }