How to post on a facebook page using PHP

Asked

Viewed 6,772 times

5

I would like to know how to make automatic posts on a particular facebook page using PHP. I consulted the documentation that facebook provides and besides being in English also did not understand enough to assemble the script. I got Appid and Appsecret.

Documentation consulted: https://developers.facebook.com/docs/reference/php/4.0.0?locale=pt_BR

Att.

  • Post here as far as you went, what was your doubt in the documentation ?

  • It does not seem broad, it seems to me a reasonable question. He is simply asking how to "post to a facebook page using PHP" which he has access to.

  • On the page you posted there is a link to an example in PHP that is what you want and more.

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 to a Facebook album. Now about leaving automatic... Use the old 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

  • Great, thank you very much, that’s right. It worked perfectly, only an authentication error is occurring, but this is something I have to do on facebook "(#200) The user hasn’t authorized the application to perform this action". The important thing is that the code you passed works. Thank you very much.

  • The posts are being made with my facebook username, however I would like the post to be made with the name of the page on which I am administrator instead of my user. Would it be possible?

  • Primeiro: Could mark as resolved? Segundo: You can use the post with the name of the page if you use all the information on your page, such as FACEBOK_ID

  • I understand, thank you. In the line "$Ret = $fb->api('/id_da_pagina/feed', 'POST', $params);" I put the page id but it is publishing with my username still. Could you tell me how I can solve this to publish on the page name? Att.

  • I am using according to your code and the publication falls as publications of visitors on the page and not as if it were posted by facebook. You’ve seen it happen ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.