使用cURL PHP发布到Facebook用户的墙上

时间:2011-04-28 05:45:58

标签: php facebook curl facebook-graph-api

我正在存储facebook用户ID并访问令牌。我可以使用此信息发布到选定用户的墙上吗?可在此处找到以下代码:http://developers.facebook.com/docs/reference/api/post/ 我只是不确定如何用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

3 个答案:

答案 0 :(得分: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 :(得分:3)

使用Facebook SDK。这比自己处理CURL要好得多。

答案 2 :(得分:3)

我尝试了cURL方法,但我不知道应该将$ 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);


?>

我成功获得了访问令牌,因此只需要这两个参数。