Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post to a Facebook page with PHP

I would like to post to my own Facebook page's wall from my website using PHP.

I have the following:

  • Facebook Application with AppID, AppSecret, ApiKey
  • Facebook Page with PageID
  • my own Facebook account - I'm the admin and the creator of the application and page mentioned above.

E.g. I write a blog post, and I'd like to get the name, the short description and a picture on my Facebook page's wall. Or I would like to publish some articles on the Facebook page every day automatically as a cron job.

Could you provide a step-by-step tutorial how to accomplish this?

I've read this article about Facebook Login:
https://developers.facebook.com/docs/facebook-login/
but I still don't know what to write in my code.


UPDATE 1

This is how I send a request for an App Access Token:

$url = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='
        .Yii::app()->params['FacebookAppID']
        .'&client_secret='
        .Yii::app()->params['FacebookSecret'];

The response is similar to this (fake symbols):

access_token=326584076429|ax3-D39YbpDcR9rMRQn_fMvNu_s

What access_token is it? Application Access Token? How to get a User Access Token?

I tried to use the access token from here:
https://developers.facebook.com/tools/explorer?method=GET&path=me%2Faccounts but I got the following error message:

An active access token must be used to query information about the current user

So how should I obtain the right access token?

UPDATE 2:

How can I get the right Facebook tokens in my application without any client interaction?
I'm the admin and the creator of the Facebook Application and the Facebook Page.

like image 473
Lari13 Avatar asked Feb 10 '12 09:02

Lari13


People also ask

How to post on Facebook automatically using PHP?

Make a PHP file and define methods to Post On Facebook We make a PHP file and save it with a name autopost.php After using above code you post will be posted automatically on your Facebook profile automatically.You may also like get facebook like, share and comment using PHP.

Is it possible to post RSS to Facebook fan page?

There is no RSS available, so do you have any example to directly post to the Facebook fan page (not user wall) using php sdk? Thank you! phpfacebookfacebook-graph-apifacebook-wallfacebook-fan-page Share Follow edited Jun 5 '16 at 8:52 Ali Sheikhpour 9,32055 gold badges3131 silver badges6969 bronze badges asked Oct 19 '11 at 8:42

How to post on Facebook without Facebook API console?

If you don't want to go Facebook API console, rather do API calls, there are some instructions. First of all, you have to have Facebook user, being admin on the page you want to post, also you have to create Facebook App in order to proceed. Do login request, to get user token:

How do I add post to my page wall using PHP?

Post to your page wall via PHP First, for this script, you'll need a server supporting curl. We start the PHP document defining the page access token and the page id that we've get in the 1st step: <?php $page_access_token = 'XXXXXXX'; $page_id = 'YYYYYYYY';


2 Answers

Step by step

  1. Authenticate a user that is a page admin (yourself)
  2. Request an extended access token (to get a 60 day variety as offline_access is gone). See https://developers.facebook.com/docs/offline-access-deprecation/
  3. Call Graph API me/accounts and search thru the resulting list to find the page you're interested in
  4. Take the page access token from the page and start using that for the calls to post
  5. It might be possible to get an extended access token for a page access token like described in step 2, please try and let us know if that can be done for page access token too.

You can experiment with the above at https://developers.facebook.com/tools/explorer

Happy Coding!

EDIT

For getting an access token without dialogs for any user, you can use https://developers.facebook.com/tools/access_token/ to get an access token.

like image 155
DMCS Avatar answered Sep 20 '22 18:09

DMCS


Steps:

  1. Request For manage_pages permission ( Allow this process ) :

    https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=manage_pages&response_type=token
    
  2. Get Access token from URL :
    If the administrator allows this permission. You should be redirected to URL below:

    http://YOUR_URL/#access_token=AAABY5jBXQz0BAEzNKkb6FZC22D7aOoKIfFuozIjoOpkGHRJ6SyzBvqx24JGooMc31374EdRFNXkOyLZCBzETRD9vhZAZC8MZD&expires_in=0
    

    Use Access token in the URL and you should get this:

    AAABY5jBXQz0BAEzNKkb6FZC22D7aOoKIfFuozIjoOpkGHRJ6SyzBvqx24JGooMc31374EdRFNXkOyLZCBzETRD9vhZAZC8MZD
    
  3. Check Access token using Graph API:

    https://graph.facebook.com/me/accounts?access_token=TOKEN_FROM_ABOVE
    

    Connection will return information and access token for each page.

  4. Implement it in your code:
    You can use App access token while call a Facebook Graph API method.

Update:
If you want use API method in Facebook SDK, DEPRECATED REST API or FQL Query...

You have to use users_accesstoken this way:

  1. Method 1:
    Use your account or users to login to your Facebook page with offline_access permissions and grab access_token while login success using $facebook->getAccessToken(), and save it in database so you can use it anytime.
    You can check the expiration time of the token here, token with offline_access permissions never expire except when the user changes his password or maybe anything else.

  2. Method 2:
    You can update your access_token dynamically using the code below (say goodbye to expire token). Facebook shows this solution here, it's a sample code for executing an FQL Query:

Code:

<?php
$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$my_url = 'POST_AUTH_URL';
$code = $_REQUEST["code"];

//auth user
if(empty($code)) {
    $dialog_url = 'https://www.facebook.com/dialog/oauth?client_id=' 
        . $app_id . '&redirect_uri=' . urlencode($my_url) ;
    echo("<script>top.location.href='" . $dialog_url . "'</script>");    
}

//get user access_token
$token_url = 'https://graph.facebook.com/oauth/access_token?client_id='
. $app_id . '&redirect_uri=' . urlencode($my_url) 
. '&client_secret=' . $app_secret 
. '&code=' . $code;
$access_token = file_get_contents($token_url);   
like image 30
Joko Wandiro Avatar answered Sep 17 '22 18:09

Joko Wandiro