dongxizhe9755 2015-11-08 20:16
浏览 84
已采纳

在laravel 5 facebook graph api中找不到Facebook类

I am using facebook graph api in laravel 5.1.I have installed it usnig composer command

composer require facebook/php-sdk-v4

the facebook graph api has been installed but when i create the facebook api object in laravel then it's give me this error

Class 'App\Http\Controllers\Userapp\Facebook\Facebook' not found

it is in this line

$fb = new Facebook\Facebook([

here is my Controller file

<?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; 

and the function of the controller

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();
    }

kindly help me solving this problem. i will really appreciate that Thanks

  • 写回答

1条回答 默认 最新

  • dongyuan1870 2015-11-08 21:29
    关注

    Within the index function, you are not in the global scope so that as you call the class as Facebook\Facebook, PHP thinks that you are calling Facebook\Facebook declared in the App\Http\Controllers\Userapp namespace.

    Either you need to reference them as globally like below with prepending \.

    public function index()
    {
        $fb = new \Facebook\Facebook([]);
        // your code
        catch(\Facebook\Exceptions\FacebookResponseException $e)
        // your code
        catch(\Facebook\Exceptions\FacebookSDKException $e)
        // your code
    }
    

    Or use the aliases.

    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
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题