'Class 'Facebook\\Facebook' not found\" Facebook SDK error

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

Well I am new to facebook sdk. I have being following the guideline and performing the steps as written.. but I am getting this error and I dont know why?

 Fatal error: Class 'Facebook\Facebook' not found in C:\wamp\www\index.php on line 134 

The error line code is:

<?php  $fb = new Facebook\Facebook([   'app_id' => '{app-id}',   'app_secret' => '{app-secret}',   'default_graph_version' => 'v2.2',   ]); ?> 

This is not something I have made up, this is exactly the same code mentioned in facebook guideline! What should I do?

回答1:

You need to include the autoloader first to get access to the service methods and classes (as said in the PHP SDK Documentation for Facebook API. You are trying to use a namespaced class Facebook\Facebook, to use its methods, but you don't have the class in the PHP file.

require_once 'src/Facebook/autoload.php'; //Create the Facebook service $fb = new Facebook\Facebook ([     'app_id' => '-----------------',     'app_secret' => '--------------------',     'default_graph_version' => 'v2.4'     ]); 

Somewhere in your directory (if you installed the Facebook PHP SDK) correctly, you will find the autoload.php file which automatically requires .php files that you need to use the services and methods.



回答2:

I think you need to first import that php class into your current file

Put this line at the top

use Facebook\Facebook;  


回答3:

So after doing some research of my own and having the above answer not work out for me I kept reading the Documentation which got me to this page:

https://github.com/facebook/php-graph-sdk/blob/5.5/docs/getting_started.md 

You have to install the Facebook PHP SDK in you root folder using Composer in your command prompt (on Windows). On my system it looked like this:

C:\wamp64\www>composer require facebook/graph-sdk 

Once that was done I had to add the following at the top of my script:

require_once __DIR__ . '/vendor/autoload.php'; 

I was now able to connect to the Facebook SDK.

I hope this works in the future for people who struggled with this like I did.



回答4:

Download the zip file from here : https://github.com/facebook/php-graph-sdk/archive/5.4.zip

Steps :

  1. Unzip the content (by either just double clicking on the zipped file or use any available unzipping software to unzip or decompress the downloaded file)

  2. Navigate to the "src" folder.

  3. Copy or cut the "src" folder and paste right within the folder from which you have your php files.

  4. make sure you have created "includes.php" file within your php project workspace ie. the folder in which your web page is sitting. And add this line to your "includes.php" file :

    require_once 'src/Facebook/autoload.php';

  5. Now in your php file(s) that has to do with facebook you can then add :

    require_once("includes.php");

  6. Now save your file and go into your browser and refresh.

//Do remember to keep your work organised by now referencing all files that may be needed in your project via "includes.php". This may vary for some developers, depending on how and what you are working on.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!