I'm editing a .php file that will run on my XAMPP installation. The file has to connect to Facebook when it's launched and, next, post a text message on a FB user profile.
To do this, I've followed the guidelines on this official page: https://developers.facebook.com/docs/php/gettingstarted/5.0.0
Specifically, I've follow the "Manually installing (if you really have to)" procedure. It's ok, but when I launch my script, I obtain this error:
Fatal error: Class 'Facebook' not found in C:\xampp\htdocs\Octopus\index.php on line 31
Here's my code, where I've insert some fake auth credentials:
<?php
//require __DIR__ . "/vendor/autoload.php";
//require_once __DIR__ . '\vendor\autoload.php';
//echo __DIR__ . '\vendor\autoload.php';
/*if (($loader = require_once __DIR__ . '/vendor/autoload.php') == null)
{
die('Vendor directory not found, Please run composer install.');
}
*/
// require Facebook PHP SDK
// see: https://developers.facebook.com/docs/php/gettingstarted/
// require_once("/YOUR_PATH_TO/facebook_php_sdk/facebook.php");
//require_once(__DIR__ . '\vendor\facebook\php-sdk-v4\src\facebook.php');
// initialize Facebook class using your own Facebook App credentials
// see: https://developers.facebook.com/docs/php/gettingstarted/#install
require_once __DIR__ . '/facebook-php-sdk-v4-5.0.0/src/Facebook/autoload.php';
$config = array();
//$config['appId'] = 'YOUR_APP_ID';
//$config['secret'] = 'YOUR_APP_SECRET';
$config['fileUpload'] = false; // optional
$fb = new Facebook(array(
'appId' => '43629645236452943',
'secret' => '385628354628075362783654728365',
));
//$fb = new Facebook($config);
// INIZIO SESSIONE DI REPERIMENTO TOKEN
session_start();
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSDKException;
$helper = new FacebookRedirectLoginHelper('{callback-url}', '{app-id}', '{app-secret}');
try
{
$session = $helper->getSessionFromRedirect();
}
catch(FacebookSDKException $e)
{
$session = null;
}
if ($session) {
// User logged in, get the AccessToken entity.
$accessToken = $session->getAccessToken();
// Exchange the short-lived token for a long-lived token.
$longLivedAccessToken = $accessToken->extend();
// Now store the long-lived token in the database
// . . . $db->store($longLivedAccessToken);
// Make calls to Graph with the long-lived token.
// . . .
} else {
echo '<a href="' . $helper->getLoginUrl() . '">Login with Facebook</a>';
}
// FINE SESSIONE
// define your POST parameters (replace with your own values)
// "access_token" => "YOUR_ACCESS_TOKEN", // see: https://developers.facebook.com/docs/facebook-login/access-tokens/
$params = array(
"access_token" => $accessToken, // see: https://developers.facebook.com/docs/facebook-login/access-tokens/
"message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook",
"link" => "http://www.pontikis.net/blog/auto_post_on_facebook_with_php",
"picture" => "http://i.imgur.com/lHkOsiH.png",
"name" => "How to Auto Post on Facebook with PHP",
"caption" => "www.pontikis.net",
"description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation."
);
// post to Facebook
// see: https://developers.facebook.com/docs/reference/php/facebook-api/
try {
//$ret = $fb->api('/YOUR_FACEBOOK_ID/feed', 'POST', $params);
$ret = $fb->api('/MY_FACEBOOK_PROFILE/feed', 'POST', $params);
echo 'Successfully posted to Facebook';
} catch(Exception $e) {
echo $e->getMessage();
}
?>
In this image, a screen of my root folder: Facebook path problem
Can you help me? Why the facebook object isn't instantiable? Thanks!