Facebook has now updated its
PHP SDK to version 4, and also try to update your
graph api to version 2.0 before December 25 2014 because they are
shutting down
support for 90% of previous graph API's functionality.
Now for posting to a Facebook page without log in you need a
long lived page access token and
page-id to which you are trying to post. You can find more information on page access tokens
here.
I assume that you have a Facebook app and a Facebook page. You will be posting to your Facebook pages via your Facebook app. To create an app go to
https://developers.facebook.com/ and click on
App->Create a New App. After you provide a name and namespace for you app you will be shown an app dashboard where you can see your app-id/client-id and app-secret/client-secret. Then go to
Settings and click on
Add platforms and select the platform of your choice. If you choose website then you may want to enter the App domain,site URL and mobile site URL (which in most cases will be the same e.g: http://www.example.com). After that go to App Details and provide a long description for your app, logo (1024x1024) and privacy policy URL. Also Facebook is not accepting white backgrounds for logo and the dimensions has to be exact :). You can see status of your Facebook app from
Status & Review. Now you need make your app
live, you can do that by clicking on a [
?] right next to
This app is in development mode. Now you need to provide extended permissions to your app so that your app can post on behalf of page. You can find it from
Tools->Graph API Explorer. Now click on get access token a pop up will appear where you can find extended permission from which you have to select
manage_pages,
publish_stream and
read_stream.
The following are the steps for posting to a Facebook page with out log in using long lived page access tokens. I assume that you have successfully created a Facebook app and a Facebook page before proceeding to the below steps:
Step 1:
Authorize first. Enter the following URL in your browser and specify your redirect URL.
https://graph.facebook.com/oauth/authorize?
type=user_agent&
redirect_uri=http%3A%2F%2Fwww.your-domain.com/my-redirect-url&
client_id=MY_APP_ID&
scope=manage_pages,publish_actions,status_update
MY_APP_ID is the app id. you can get if from your app dashboard. If you already haven't provided extended permissions Facebook will ask for authorization.
Now you will be redirected to your redirect URL and at the end of the URL there will be a authorization token. Now copy that access token and go to step 2
Step 2:
Request for long term access token. Enter the following URL in your browser and specify your App id, App Secret and access token received from Step 1.
https://graph.facebook.com/oauth/access_token?
client_id=paste_your_app_id_here
&client_secret=paste_your_app_secret_here
&grant_type=fb_exchange_token&fb_exchange_token=paste_your_authorization_access_token_from_step1_here
Now you will get another access token which will be displayed in browser. Copy that access token.
Step 3:
Retrieve long term page access token. Enter the following URL in your browser and specify your access token received from Step 2.
https://graph.facebook.com/me/accounts?access_token=paste_your_access_token_from_step2_here
Now you will get a JSON result as below:
{
"data": [
{
"category": "some_category",
"name": "your name",
"access_token": "a_very_long_lived_page_access_token_here",
"perms": [
"ADMINISTER",
"EDIT_PROFILE",
"CREATE_CONTENT",
"MODERATE_CONTENT",
"CREATE_ADS",
"BASIC_ADMIN"
],
"id": "your_app_id"
}
],
"paging": {
"next": "https://graph.facebook.com/607528643/accounts?access_token=...."
}
Now copy the
access_token and use it to post to your Facebook page.
PHP example :
<?php
function facebook_post_to_page_via_long_access_tokens(){
$accessToken = 'a_very_long_lived_page_access_token_here';
$message = 'HelloWorld!!!';
$link = 'http://some_link_to_share';
$picture = 'url_to_picture_here';
//Post via curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://graph.facebook.com/v2.0/your_page_id_here/feed");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"message=$message&picture=$picture&link=$link&access_token=$accessToken");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
return $server_output;
}
?>
Now you will get a post id, save it if you need it for any future use. Please don't hesitate to point out of any bugs or false information( if you feel so :( ) in this post. Hope this becomes helpful to anyone else. Enjoy!!!.