我在laravel 5.1中使用facebook graph api。我已经安装了它usnig composer command
composer require facebook/php-sdk-v4
已经安装了facebook graph api 但是当我在laravel中创建facebook api对象时,它会给我这个错误
Class 'App\Http\Controllers\Userapp\Facebook\Facebook' not found
就在这一行
$fb = new Facebook\Facebook([
这是我的控制器文件
<?php namespace App\Http\Controllers\Userapp;
require_once( base_path('vendor/facebook/php-sdk-v4/src/Facebook/autoload.php') );
// Facebook Requires
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;
和控制器的功能
public function index()
{
// init app with app id and secret
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.5',
//'default_access_token' => '{access-token}', // optional
]);
try {
// Get the Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me', '{access-token}');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
}
请帮助我解决这个问题。 我真的很感激 感谢
答案 0 :(得分:4)
在index函数中,您不在全局范围内,因此当您将该类称为Facebook\Facebook
时,PHP认为您正在调用Facebook\Facebook
命名空间中声明的App\Http\Controllers\Userapp
。
您需要像下面一样全面引用它们,并预先加\
。
public function index()
{
$fb = new \Facebook\Facebook([]);
// your code
catch(\Facebook\Exceptions\FacebookResponseException $e)
// your code
catch(\Facebook\Exceptions\FacebookSDKException $e)
// your code
}
或使用别名。
use Facebook\Facebook as Facebook;
use Facebook\Exceptions\FacebookResponseException as FacebookResponseException
use Facebook\Exceptions\FacebookSDKException as FacebookSDKException
public function index()
{
$fb = new Facebook([]);
// your code
catch(FacebookResponseException $e)
// your code
catch(FacebookSDKException $e)
// your code
}