How to post to a facebook page using PHP

5

I would like to know how to make automatic postings on a particular facebook page using PHP. I consulted the documentation that facebook offers and besides being in English I also did not understand enough to assemble the script. I already have AppId and AppSecret.

Documentation consulted: link

Att.

    
asked by anonymous 19.02.2015 / 02:12

1 answer

8

Here is the code you need to share a link to Facebook using PHP. With small changes you can use this code to leave only one message (no link), or upload a photo into a Facebook album. Now on automatic leave ... Use the old and good JavaScript logic!

<?php
// requer Facebook PHP SDK
// veja: https://developers.facebook.com/docs/php/gettingstarted/
require_once("/SEU_PATH_TO/facebook_php_sdk/facebook.php");

// initialize Facebook class using your own Facebook App credentials
// see: https://developers.facebook.com/docs/php/gettingstarted/#install
$config = array();
$config['appId'] = 'SEU_APP_ID';
$config['secret'] = 'SEU_APP_SECRET';
$config['fileUpload'] = false; // opcional

$fb = new Facebook($config);

// definindo os parâmetros do POST (substitua os valores)
$params = array(
  "access_token" => "SEU_TOKEN", // veja: https://developers.facebook.com/docs/facebook-login/access-tokens/
  "message" => "Estou usando auto post com #php #facebook",
  "link" => "http://qualquer.br",
  "picture" => "http://i.imgur.com/lHkOsiH.png",
  "name" => "Auto post com PHP",
  "caption" => "www.qualquer.com.br",
  "description" => "Automaticamente postar no Facebook com PHP usando Facebook PHP SDK."
);

// post para Facebook
// veja: https://developers.facebook.com/docs/reference/php/facebook-api/
try {
  $ret = $fb->api('/SEU_FACEBOOK_ID/feed', 'POST', $params);
  echo 'Postado no face!';
} catch(Exception $e) {
  echo $e->getMessage();
}
?>

Settings:

  • You have to renew Access Token after expiration (about 60 days).

  • You have to get your Facebook ID, either for your personal profile or for your Fan Pages or Business Pages.

  • You have to create a Facebook application in order to use the Facebook API

19.02.2015 / 15:56