2013-01-20 49 views
2

張貼新的留言我有一個食譜網站,也是網站在Facebook上的羣組有幾千朋友。我想在小組時間表上發佈新的食譜。例如,我可以每1小時使用一次cron。如何在羣組牆

require_once('facebook.php'); 

    $config = array(
     'appId' => 'xxx', 
     'secret' => 'xxx', 
    ); 

    $facebook = new Facebook($config); 

    // If not, we'll get an exception, which we handle below. 
    try { 
     $ret_obj = $facebook->api('/page_id/feed', 'POST', 
            array(
            'link' => 'www.example.com', 
            'message' => 'Posting with the PHP SDK!' 
           )); 
     echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>'; 

    } catch(FacebookApiException $e) { 
     echo "error type: ". $e->getType()."<br />"; 
     echo "error mesage: ". $e->getMessage()."<br />"; 
    } 

我該如何更改我的腳本以便能夠在cron守護程序的組頁面上發佈?

我改劇本一點,現在我有這樣的錯誤: 錯誤類型:OAuthException 錯誤mesage:(#200)的用戶沒有授權的應用程序來執行此操作

回答

0

嘗試:

$config = array(
    'appId' => 'xxx', 
    'secret' => 'xxx', 
    'cookie' => true 
); 

設置一個cookie應該讓你登錄

+0

這並沒有改變任何東西。我仍然無法發佈消息... –

+0

嗯,我現在看到了問題。您嘗試發佈您的消息 - 如果這不起作用,您登錄。該如何工作? –

+0

我改變了腳本,現在我有更好的錯誤信息。 –

0

正如文件中here說:

Users can post a link on the Group's wall by issuing an HTTP POST request to /GROUP_ID/feed with the publish_actions permissions and the following parameters. (publish_stream will also work, but you should use publish_actions .) This requires a user access_token .

所以,你需要做的只是張貼(運行腳本的用戶):

  1. publish_actions許可
  2. 用戶access_token

我們用這個作爲的cronjob,我通常做以下事情:

  1. 授權用戶(誰擁有權利發佈到這個組)與所需的權限
  2. extend his token
  3. 存儲此令牌,在DB
  4. 使用令牌發佈
  5. 留意到期變量之前,也就是說,令牌3天到期 - >再次發送電子郵件給用戶「登錄」到您的應用程序,然後去到#3
  6. 總是捕捉任何錯誤,而張貼並將其記錄;你可以檢查是否錯誤是關於訪問令牌,因此去-#5
+0

一些示例代碼在這裏非常精彩。要獲得用戶的access_token,你可以使用這個方法:'$ facebook-> getAccessToken()'。你必須首先實例化$ facebook對象。 '$ facebook = new Facebook($ config);' –