2015-09-04 36 views
2

我正在開發一個需要Facebook集成的PHP項目。所以在我做代碼之前,我正在使用Facebook Graph API瀏覽器工具(https://developers.facebook.com/tools/explorer)測試它。我現在正在做的是。如何使用CURL在Facebook上發佈帖子,而不使用PHP中的SDK

第一步驟

Get the user access_token by using the button at the top left. 

第二步驟

Make a GET request to "me/accounts" to get the page token and page id. 

第三步驟

Make a POST request to "{page_id}/feed" with the fields message={message} and access_token={page_token} 

它工作完美,張貼在我的Facebook粉絲頁。但是,當我嘗試用這樣的PHP代碼代替「第三步」時

$data['message'] = "my message"; 


$data['access_token'] = $page_access_token; //page token from 2nd step 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://graph.facebook.com/{page_id}/feed'); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch,CURLOPT_POSTFIELDS, $data); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); 
curl_setopt($ch, CURLOPT_POST, 1); 


$resp = curl_exec($ch); 

curl_close($ch); 

$data_resp = json_decode($resp); 

print_r($data_resp); 

它向我顯示這個錯誤。

stdClass Object ([error] => stdClass Object ([message] => (#200) Permissions error [type] => OAuthException [code] => 200)) 

我設置的manage_pages,publish_pages,publish_actions權限

+0

調試你的'access_token'並檢查它是否獲得了所需的權限。 https://developers.facebook.com/tools/debug/ – Criesto

回答

-1

看一看該文檔在

個權限

  • publish_actions權限的用戶訪問令牌可以用來代指人的發佈新文章。帖子將出現在用戶的聲音中。
  • 具有publish_pages權限的頁面訪問令牌可用於代表該頁面發佈新帖子。帖子將出現在頁面的聲音中。

您應該通過

運行用於訪問令牌,看它是否包含了適當的權限(S)。

+0

是的。令牌很好,它包含了所有需要的權限。 –

2

傳遞數組CURLOPT_POSTFIELDS意味着cURL將發送請求作爲內容類型multipart/form-data - 但您想要application/x-www-form-urlencoded代替。

在您的$data數組上使用http_build_query,並將結果字符串用於CURLOPT_POSTFIELDS

相關問題