问题
Trying to import and use the FacebookRedirectLoginHelper class of the facebook php sdk in my AppController but getting a Class 'FacebookRedirectLoginHelper' not found Exception.
App::import('Vendor', 'facebook', array('file' => 'facebook-php-sdk-v4-master' . DS . 'src' . DS . 'Facebook' . DS . 'FacebookRedirectLoginHelper.php'));
$helper = new FacebookRedirectLoginHelper($redirect_url, 'xxxx', 'yyyyy');
Any idea? Thanks in advance
回答1:
I've got the same problem before and got it working now, not sure if I'm doing it in a proper way or not but it's working for me.
1. Place this in your AppController (can be in function like beforeFilter or top of code)
<?php App::import("Vendor", "FacebookAuto", array("file" => "facebook-php-sdk-v4-4.0-dev/autoload.php")); ?>
2. Place as well the 'use' keyword in any Controller you want to call the facebook class
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
?>
3. You definitely have to do session_start() with FacebookRedirectLogin Helper
4. Start your FacebookSession and prepare your Redirect URL, and it will work nicely
<?php
FacebookSession::setDefaultApplication("APP_ID", "SECRET_ID");
$facebookRedirect = Router::url('/controllers/action', true);
$helper = new FacebookRedirectLoginHelper($facebookRedirect);
?>
Hope it will solve your problem!
回答2:
I had the same issue and tried most of the solutions here. In the end nothing worked because I was point to the wrong build and I was not telling CakePHP to use the Composer Autoloader.
In my composer.json I changed:
"facebook/php-sdk-v4": "dev-master"
to
"facebook/php-sdk-v4" : "4.0.*"
and at the top of my Config/bootstrap.php
file I added
<?php
// Load Composer autoload.
require APP . 'Vendor/autoload.php';
// Remove and re-prepend CakePHP's autoloader as Composer thinks it is the
// most important.
// See: http://goo.gl/kKVJO7
spl_autoload_unregister(array('App', 'load'));
spl_autoload_register(array('App', 'load'), true, true);
The proper composer entry was found here: https://developers.facebook.com/docs/php/gettingstarted/4.0.0
The composer autoloder with CakePHP was found here: http://book.cakephp.org/2.0/en/installation/advanced-installation.html#installing-cakephp-with-composer
Now from anywhere in my app I can do this:
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
FacebookSession::setDefaultApplication('app_id', 'app_secret');
Hope it helps and that this issue does not deter you from continuing to use Composer.
回答3:
I also have this problem and it seems I can't solve it. The work around for me is to use older version at:FB SDK V3
回答4:
Here's how I have it working.
Below is the directory structure of the Vendor Dir ( My use-case is inside a plugin but all in all the same as if inside your apps Vendor Dir)

Then, I use a helper class ( not a CakePHP Helper, just a class that helps me out ;-) ) to include the SDK php files.
*NOTE: The order the SDK file are included is important to avoid race conditions in the class declarations.
You can then see how I request the login URL.
This is just a simple example of how I have it working.
Your milage may vary, however, should get your ball rollin
<?php
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
class FacebookApiHelper {
public static $init = false;
public static function init() {
if(!self::$init) {
foreach(array(
'GraphObject.php',
'FacebookHttpable.php',
'FacebookCurlHttpClient.php',
'FacebookSDKException.php',
'FacebookRequestException.php',
'FacebookAuthorizationException.php',
'FacebookCanvasLoginHelper.php',
'FacebookClientException.php',
'FacebookCurl.php',
'FacebookJavaScriptLoginHelper.php',
'FacebookOtherException.php',
'FacebookPageTabHelper.php',
'FacebookPermissionException.php',
'FacebookRedirectLoginHelper.php',
'FacebookRequestException.php',
'FacebookRequest.php',
'FacebookResponse.php',
'FacebookServerException.php',
'FacebookSession.php',
'FacebookThrottleException.php',
'GraphAlbum.php',
'GraphLocation.php',
'GraphSessionInfo.php',
'GraphUser.php',
)
as $file) {
require_once __DIR__ . DS . "/Facebook/{$file}";
}
FacebookSession::setDefaultApplication(
Configure::read("UserManager.FacebookApi.app_id"),
Configure::read("UserManager.FacebookApi.app_secret")
);
self::$init = true;
}
}
public static function getLoginUrl($callback_url = "/user_manager/login/handle_facebook_login_redirect") {
self::init();
$helper = new FacebookRedirectLoginHelper($callback_url);
return $helper->getLoginUrl();
}
}
回答5:
Hi, I couldn't get this problem solved with any of answerers you provided, but I managed to find my own solution with the help of my colleague.
Versions I'm using here are: CakePHP 2.3, Facebook PHP SDK 4.0.0, PHP 5.4.
- I downloaded Facebook SDK from developers.facebook.com.
- I put src-folder files in cakephp app/Vendor/FacebookSDK/src.
In my controller-file called DashboardController I'm now including Facebook SDK like this:
App::import('Vendor', 'FacebookRedirectLoginHelper', array('file' => 'FacebookSDK' . DS . 'src' . DS . 'FacebookRedirectLoginHelper.php')); App::import('Vendor', 'FacebookSDKException', array('file' => 'FacebookSDK' . DS . 'src' . DS . 'FacebookSDKException.php')); App::import('Vendor', 'FacebookSession', array('file' => 'FacebookSDK' . DS . 'src' . DS . 'FacebookSession.php')); App::import('Vendor', 'FacebookRequest', ['file' => 'FacebookSDK' . DS . 'src' . DS . 'FacebookRequest.php']); App::import('Vendor', 'FacebookRequestException', ['file' => 'FacebookSDK' . DS . 'src' . DS . 'FacebookRequestException.php']);
And use it like this in my DashboardsController Facebook-method:
$helper = new \Facebook\FacebookRedirectLoginHelper(Router::url('/', true), $appId, $secret);
Notice that \Facebook\ , that is the namespace of class FacebookRedirectLoginHelper. Without this namespace, code could find the file but not the class inside the file.
来源:https://stackoverflow.com/questions/24539855/how-to-import-the-facebook-php-sdk-v4-in-cakephp-2-4