错误:在 facebook PHP SDK 中找不到“Facebook\FacebookSession”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23569934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: Class 'Facebook\FacebookSession' not found with the facebook PHP SDK
提问by Paul Fournel
I am having a hard time with facebook's SDK documentation. I downloaded the SDK from Github and added it into my PHP project.
我在使用 facebook 的 SDK 文档时遇到了困难。我从 Github 下载了 SDK 并将其添加到我的 PHP 项目中。
Here is the file system:
这是文件系统:
?? ├── Facebook
?? │?? ├── FacebookAuthorizationException.php
?? │?? ├── FacebookCanvasLoginHelper.php
?? │?? ├── FacebookClientException.php
?? │?? ├── FacebookJavaScriptLoginHelper.php
?? │?? ├── FacebookOtherException.php
?? │?? ├── FacebookPermissionException.php
?? │?? ├── FacebookRedirectLoginHelper.php
?? │?? ├── FacebookRequest.php
?? │?? ├── FacebookRequestException.php
?? │?? ├── FacebookResponse.php
?? │?? ├── FacebookSDKException.php
?? │?? ├── FacebookServerException.php
?? │?? ├── FacebookSession.php
?? │?? ├── FacebookThrottleException.php
?? │?? ├── GraphLocation.php
?? │?? ├── GraphObject.php
?? │?? ├── GraphSessionInfo.php
?? │?? ├── GraphUser.php
?? │?? └── fb_ca_chain_bundle.crt
?? └── test.php
here is my code so far:
到目前为止,这是我的代码:
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('*******','******');
$helper = new FacebookRedirectLoginHelper('http://isgeek.eu/fb/FaRepost/return.php');
$loginUrl = $helper->getLoginUrl();
// Use the login url on a link or button to redirect to Facebook for authentication
I get this error
我收到这个错误
Fatal error: Class 'Facebook\FacebookSession' not found in /homepages/2/d184071366/htdocs/isgeek/fb/FaRepost/test.php on line 9
At updated my PHP version, so the issue does not comme from here. It seems like the PHP files are not found. I read this question (Facebook SDK v4 for PHP Minimal Example) but it does not help.
在更新我的 PHP 版本时,所以问题不是来自这里。似乎找不到 PHP 文件。我读了这个问题(Facebook SDK v4 for PHP Minimal Example),但它没有帮助。
Where does this comme from?
这是从哪里来的?
回答by rajesh ujade
- PHP Version 5.4.0 or higher is required.
- Facebook uses Implementations of PSR-4. Hence You dont have to use require or require_once or include or include_once.
- In PSR-4, you just need packagename(namespace) i.e. directory name and class file name only.It will register classes dynamically from given package name.Ex.:-
use packaname\classname
. - You will find the file autoload.phpin Facebook SDK Autoloadroot directory.
use
is used to load dynamic classes usingspl_autoload_register
- Facebook register all library using
autoload.php
orautoload_fb.php
- You have to find autoload.php in your downloaded library like
facebook-php-sdk-v4-4.0-dev/
. - If you just wants to use Facebooklibrary from download source.Then you have to copy autoload.phpin your root directory or in Facebook directory.
- defined constant for
FACEBOOK_SDK_V4_SRC_DIR
i.e. path of the facebook library - You need to do as below to usein php
- 需要 PHP 5.4.0 或更高版本。
- Facebook 使用PSR-4 的实现。因此,您不必使用require 或 require_once 或 include 或 include_once。
- 在 PSR-4 中,你只需要 packagename(namespace) 即目录名和类文件名。它会从给定的包名动态注册类
use packaname\classname
。例如:- 。 - 您将在Facebook SDK Autoload根目录中找到autoload.php文件。
use
用于加载动态类使用spl_autoload_register
- Facebook 使用
autoload.php
或注册所有图书馆autoload_fb.php
- 您必须在下载的库中找到 autoload.php,例如
facebook-php-sdk-v4-4.0-dev/
. - 如果您只是想从下载源使用Facebook库。那么您必须将autoload.php复制到您的根目录或 Facebook 目录中。
- 为
FACEBOOK_SDK_V4_SRC_DIR
ie facebook 库的路径定义常量 - 您需要执行以下操作才能在 php 中使用
Note:I have copied /var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/src/Facebook
directory and /var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/autoload.php
file in root directory /var/www/stack/24006673/
注意:我已经复制了根目录下的/var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/src/Facebook
目录和/var/www/stack/24006673/facebook-php-sdk-v4-4.0-dev/autoload.php
文件/var/www/stack/24006673/
define('FACEBOOK_SDK_V4_SRC_DIR','/var/www/stack/24006673/Facebook/');
require_once("autoload.php");
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
FacebookSession::setDefaultApplication('YOUR_APP_ID','YOUR_APP_SECRET');
回答by Paul Fournel
I found the solution here
我在这里找到了解决方案
I did not code in php for some time now and things have changed. use Facebook\FacebookSession;
is not enough. You need to add a require_once
too.
我已经有一段时间没有在 php 中编写代码了,事情已经发生了变化。use Facebook\FacebookSession;
是不足够的。你也需要添加一个require_once
。
require_once( 'Facebook/FacebookSession.php' );
Edit: for a more detailed solution, please checkout the answer below.
编辑:有关更详细的解决方案,请查看下面的答案。
回答by www.amitpatil.me
This code worked for me
这段代码对我有用
session_start();
require_once( 'Facebook/FacebookSession.php' );
require_once( 'Facebook/FacebookRedirectLoginHelper.php' );
require_once( 'Facebook/FacebookRequest.php' );
require_once( 'Facebook/FacebookResponse.php' );
require_once( 'Facebook/FacebookSDKException.php' );
require_once( 'Facebook/FacebookRequestException.php' );
require_once( 'Facebook/FacebookAuthorizationException.php' );
require_once( 'Facebook/GraphObject.php' );
require_once( 'Facebook/Entities/AccessToken.php' );
require_once( 'Facebook/HttpClients/FacebookHttpable.php' );
require_once( 'Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( 'Facebook/HttpClients/FacebookCurl.php' );
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookCurl;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('XXXX', 'XXXXXXXXXXXX');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://localhost/demo/demo2/demo2.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo print_r( $graphObject, 1 );
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}
回答by Darius Miliauskas
You should be careful with all the paths!
你应该小心所有的路径!
I cannot see "autoload.php" in your file system. In my case I put the content of PHP SDK in the directory "fb", and use the paths (added DIR. into the "define" line in comparison with the example of https://developers.facebook.com/docs/php/gettingstarted/4.0.0)
我在您的文件系统中看不到“autoload.php”。在我的情况下,我将 PHP SDK 的内容放在目录“fb”中,并使用路径(与https://developers.facebook.com/docs/php的示例相比,将DIR.添加到“define”行中/gettingstarted/4.0.0)
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/fb/src/Facebook/');
require __DIR__ . '/fb/autoload.php';
echo FACEBOOK_SDK_V4_SRC_DIR; //to check if the paths are correct
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
<...>
It helped to avoid that kind of the error.
它有助于避免这种错误。
回答by Arvind Bhardwaj
No need to use require
or include
.
I solved it. Just use following line at the top of your script:
无需使用require
或include
。我解决了。只需在脚本顶部使用以下行:
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/facebook-php-sdk-v4/src/Facebook/');
and you are done.
你就完成了。
回答by Gaurav
require_once 'Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '{app_id}',
'app_secret' => '{app_secret}',
'default_graph_version' => 'v2.2',
]);
-just add this code .it works. enter your app_id and app_secret of your facebookAPP
- 只需添加此代码即可。输入你的facebookAPP的app_id和app_secret
回答by rustyx
Rename all Facebook files and directories to lower case. Auto-loading on *nix automatically lowercases all files names.
将所有 Facebook 文件和目录重命名为小写。*nix 上的自动加载会自动小写所有文件名。