14

I'm storing facebook userid's and access tokens. Can i post to a selected user's wall with this information? The following code is found here: http://developers.facebook.com/docs/reference/api/post/ I'm just not sure how to run it with 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
Adam Merrifield
  • 10,307
  • 4
  • 41
  • 58

3 Answers3

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);
Flask
  • 4,966
  • 1
  • 20
  • 39
  • when ever I run this code from the browser it works. But when I run this in console i get {"error":{"message":"(#200) The user hasn't authorized the application to perform this action","type":"OAuthException","code":200}} – themhz Aug 23 '13 at 21:52
  • Thanks and to add what I figured out: to upload a picture from your server, change the request url to: https://graph.facebook.com/fbnameorid/photos and add an array item called 'file' with value '@'. [path to file] – ssaltman Nov 04 '13 at 16:59
3

I tried the cURL method but I don't know what should be $action_name and $action_link changed to.

<?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);


?>

I get the access token successfully, so only these 2 parameters are what I need.

Lexx
  • 705
  • 2
  • 9
  • 19
  • Just add something like:` $action_name = 'Register Now'; $action_link = 'https://example.com/register/';` – Andy Gee Sep 22 '13 at 21:03
3

Use Facebook SDK. It's much better than handling CURL by yourself.

Dr McKay
  • 2,548
  • 2
  • 21
  • 26
  • 11
    Perhaps you could note what specific functions in the Facebook SDK could replace this code instead of pointing to the entire SDK. – joe Jan 28 '13 at 17:11