1

I'm the admin of FB app and FB page (but this page on a separate account). How do I can post something to this page's wall via this FB app using FB API and PHP (in order to be able to do it with CRON)? Is that possible? Thank you in advance for your answers!

Red October
  • 689
  • 2
  • 12
  • 31

3 Answers3

1

Yes it is possible.

First of all, to post on a page on its behalf is done using the page access token.

Get a normal token from your app (may be using Graph API Explorer directly by selecting your app from top-right drop down menu) with the permission: manage_pages, then follow the steps I've mentioned here: https://stackoverflow.com/a/18322405/1343690 - this will get you a never-expiring page access token.

Save it somewhere and use with your cron-job while posting. Code for posting-

$url = 'https://graph.facebook.com/{page-id}/feed';
$attachment =  array(
    'access_token'  => $page_access_token,
    'message'  => '{your-message}'
);
$result = PostUsingCurl($url, $attachment);
$result = json_decode($result, TRUE);
if( isset($result['error']) ) {
   echo "Error: ".$result['error']['message']."<br/>";
}
else{
  echo "Feed posted successfully!<br/>";
}

function PostUsingCurl($url, $attachment)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    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_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    curl_close ($ch);

    return $result;
}
Community
  • 1
  • 1
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
0

Add Api to your page

<script id="facebook-jssdk" src="//connect.facebook.net/en_US/all.js#xfbml=1"></script>`

click function to call fb page

$('#facebook').click(function(){
  FB.init({
    appId: 12345, // your app ID
    status: true,
    cookie: true
  });
  FB.ui({
    method: 'feed',
    name: "post name",
    link: "http://postlink.com,
    //picture: "http:/imageurl.com,
    description: "this is the body of the text"
  });

})  
m59
  • 43,214
  • 14
  • 119
  • 136
im_benton
  • 2,541
  • 4
  • 21
  • 30
  • Thanks for your answer, but that's not exactly what I wanted. Your code works, but it puts a message to the wall of my personal account, not a page's wall. Also I forgot to mention that I want to do it server-based, via PHP. I've updated my question accordingly. – Red October Dec 14 '13 at 22:20
0

I use this personally. Though you will need to already have an access_token generated. If you do not, you could use Facebook's Graph Explorer tool to grant your account the appropriate permissions.

$attachment =  array(
"access_token" => $fb_token,
"link" => "$postLink",
"name" => "$postName",
"description" => "$postDescription",
"message" => "$postMessage",
"fb:explicitly_shared" => true
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/'.$fb_page_id.'/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_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //to suppress the curl output 
$result = curl_exec($ch);
curl_close ($ch);

Hope this helps out!

socca1157
  • 365
  • 3
  • 17