dongyan2267 2015-10-28 11:34
浏览 65

修复Facebook sdk路径

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!

  • 写回答

1条回答 默认 最新

  • dongmei8071 2015-10-28 11:42
    关注

    I think you should also add the namespace.

    The code for it in one of my project:

    $fb = new Facebook\Facebook([
        'app_id' => $app_id,
        'app_secret' => $app_secret,
        'default_graph_version' => 'v2.2',
    ]);
    

    Hope it helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧