1

I want to be able for my web site to post a message on the fan page of the website.

So I use this code I found:

<?php
require_once("assets/facebook.php");

$facebook = new Facebook(array(
'appId'  => '471898006354908', // Fake
'secret' => 'd2f7fb2dbc0ab7f42bc1c4337ab041b1', // Fake
'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);

?>

Each time I execute the page it echoes me something like this:

471898006354908|d2f7fb2dbc0ab7f42bc1c4337ab041b1

But it don't post anything on my fan page ?

Any help please ?

teamo
  • 443
  • 1
  • 7
  • 16

2 Answers2

2

If that is all the code you found, then you are missing one important part: the login process. It´s explained in detail in the docs: https://developers.facebook.com/docs/facebook-login/v2.4

Right now it seems that you are only using an App Access Token, but you need a User Access Token that is authorized with the publish_actions permission. Make sure you understand all the different Tokens, here are some links about those:

Don´t forget to read about Login Review too, if you want to go public with your App: https://developers.facebook.com/docs/facebook-login/review

Using your own CURL calls is perfectly fine btw, i would not suggest using the PHP SDK for small projects because that´s just overkill and the PHP SDK uses CURL too. After all it´s just a bunch of PHP Classes. Just make sure you don´t prefill the message parameter, because that´s not allowed according to the platform policy.

andyrandy
  • 72,880
  • 8
  • 113
  • 130
-2

Use the Facebook API / SDK.

This is probably not permitted in ToS and actively prevented. They detect the agent and use other means to prevent you accomplishing this with cURL.

Or you need to at least jump through a couple authentication hoops probably to prevent abuse. Check this past post and please close this if you deem your question to be a dupe. Post to a Facebook user's wall with cURL PHP

Community
  • 1
  • 1
ficuscr
  • 6,975
  • 2
  • 32
  • 52
  • Ok I will try. Do you have any tutorial ? – teamo Aug 21 '15 at 20:30
  • I'm sure there are many tutorials on the web. Just be sure to check with FB actual documentation too to ensure you are using the latest/current code. Start here: https://github.com/facebook/facebook-php-sdk-v4 then google for examples... Good luck – ficuscr Aug 21 '15 at 20:34
  • 1
    it is completely irrelevant if you use curl on your own or the facebook sdk (which uses curl too). it is only important not to prefill the message parameter. – andyrandy Aug 21 '15 at 20:52
  • I stand corrected. Honestly didn't read the question and assumed was trying to emulate actions on FB page and not even using the graph service. I'd personally still use the SDK to make code easier to maintain if FB changes things up down the road. – ficuscr Aug 21 '15 at 20:59