2011-04-28 94 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

相关问题