Reputation: 689
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!
Upvotes: 1
Views: 1208
Reputation: 20753
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;
}
Upvotes: 1
Reputation: 2621
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"
});
})
Upvotes: 0
Reputation: 364
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!
Upvotes: 0