2011-04-28 91 views
14

我正在存儲Facebook用戶名和訪問令牌。我可以通過這些信息發佈到選定用戶的牆上嗎?下面的代碼在這裏找到:http://developers.facebook.com/docs/reference/api/post/ 我只是不知道如何運行它與PHP。用cURL發佈到Facebook用戶的牆上PHP

curl -F 'access_token=$accessToken' \ 
    -F 'message=Check out this funny article' \ 
    -F 'link=http://www.example.com/article.html' \ 
    https://graph.facebook.com/$facebookid/feed 
+0

如何使用PHP的[cURL API](http://ca2.php.net/manual/en/book.curl.php)? – zneak 2011-04-28 05:50:18

+0

我懷疑這會工作,但我會嘗試:) – Kumar 2011-04-28 05:54:57

回答

30
$attachment = array(
'access_token' => $token, 
'message' => $msg, 
'name' => $title, 
'link' => $uri, 
'description' => $desc, 
'picture'=>$pic, 
'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/fbnameorid/feed'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 
$result = curl_exec($ch); 
curl_close ($ch); 
+1

應該在這段代碼中有一個'if'語句? – pepe 2011-06-14 15:33:12

+0

當我運行這個代碼從它的瀏覽器工作。但是當我在控制檯中運行時,我得到{「error」:{「message」:「(#200)用戶沒有授權應用程序執行此操作」,「type」:「OAuthException」,「code」: 200}} – themis 2013-08-23 21:52:11

+0

謝謝,並添加我想出的:從服務器上傳圖片,將請求url更改爲:https://graph.facebook.com/fbnameorid/photos,並添加一個名爲'file'的數組項值'@'。 [文件路徑] – ssaltman 2013-11-04 16:59:41

2

使用Facebook SDK。這比自己處理CURL要好得多。

+10

也許你可以注意到Facebook SDK中的特定功能可以代替這個代碼而不是指向整個SDK。 – joe 2013-01-28 17:11:55

3

我試着捲曲方法,但我不知道什麼應該是$ ACTION_NAME和$ ACTION_LINK改變。

<?php 
require_once("facebooksdk/facebook.php"); 
require_once('config.php'); 

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

$access_token = $facebook->getAccessToken(); 
echo $access_token; 
$msg = "testmsg"; 
$title = "testt"; 
$uri = "http://somesite.com"; 
$desc = "testd"; 
$pic = "http://static.adzerk.net/Advertisers/d18eea9d28f3490b8dcbfa9e38f8336e.jpg"; 
$attachment = array(
'access_token' => $access_token, 
'message' => $msg, 
'name' => $title, 
'link' => $uri, 
'description' => $desc, 
'picture'=>$pic, 
'actions' => json_encode(array('name' => $action_name,'link' => $action_link)) 
); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/me/feed'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output 
$result = curl_exec($ch); 
curl_close ($ch); 


?> 

我得到的訪問令牌成功,所以只有這兩個參數是什麼,我需要。

+0

只需添加如下內容:' \t $ action_name ='立即註冊'; \t $ action_link ='https://example.com/register/';' – 2013-09-22 21:03:03

相關問題