How to post to a Facebook page with PHP

后端 未结 3 766
南方客
南方客 2020-12-08 23:01

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, A
3条回答
  •  粉色の甜心
    2020-12-08 23:37

    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:

    top.location.href='" . $dialog_url . "'");    
    }
    
    //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);   
    

提交回复
热议问题