Publish Facebook page SDK php

Asked

Viewed 203 times

1

How to post to a facebook page through PHP?

I’m trying to:

    require 'facebook_php_sdk/Facebook.php';

// ATENCAO, configurar os parametros abaixo
$APP_ID = "12345678"; // id da app
$SECRET = "***************************"; // secret da app
$PERMS = "publish_actions,manage_pages";

// objeto do facebook
$facebook = new Facebook(array(
  'appId'  => $APP_ID,
  'secret' => $SECRET,
));

// monta URL atual
$my_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

// obtem CODE da autenticacao OAUTH
$code = $_REQUEST['code'];
if(empty($code)) {
        $dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
               . $APP_ID . "&redirect_uri=" . urlencode($my_url)
               . "&scope=$PERMS";

        header("Location: $dialog_url");
        exit;
}

// com o CODE vamos gerar a URL para obter o access token do usuario
$token_url = "https://graph.facebook.com/oauth/access_token?"
       . "client_id=" . $APP_ID . "&redirect_uri=" . urlencode($my_url)
       . "&client_secret=" . $SECRET . "&code=" . $code;

$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);

// printando o access token e quando ele ira expirar
echo "Access Token: ";
echo $params['access_token'];
echo "<br />";
if (!empty($params["expires"])) {
        echo "Ir&aacute; expirar em: " . date("d/m/Y H:i:s", time() + $params["expires"]);
}

I just keep getting the bug:

Parse error: syntax error, unexpected T_STRING in /home/igosp794/public_html/modulo/administrativo/facebook_php_sdk/Facebook.php on line 24

On line 24 we have:

namespace Facebook
  • What’s on the line 24 of the file /home/igosp794/public_html/module/administrative/facebook_php_sdk/Facebook.php ?

  • Just: namespace Facebook;

  • It may be that the version of php you are using does not support the use of namespace. See if your php version is higher than 5.3.

  • Oops buddy, forgive me the delay, I updated the php of the server, I’m with the error Fatal error: Class 'Facebook' not found in face.php on line 20, line 20 has: $facebook = new Facebook(array(. In require_once either with autoload.php or Facebook.php, the error is the same.

1 answer

0

From what I can tell you’re not using the Facebook sdk autoload. Even solving the error described by you is unlikely to work. The only require_once you should do is the autoload.php file from the facebook sdk. It will automatically include the classes necessary for the operation of the application. All this assuming you are using the latest version of sdk.

In that link you can see a tutorial on how to do the correct installation of the facebook sdk.

Browser other questions tagged

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