[英]Codeigniter and Facebook API
I have been trying to get the Facebook SDK to work with Codeigniter but it's just not working. 我一直试图让Facebook SDK与Codeigniter合作,但它只是不起作用。 I've Googled the problem for about 8 hours now so I've finally given in and come here.
我已经用Google搜索了大约8个小时的问题,所以我终于放弃了来到这里。
1) I placed the facebook.php , base_facebook.php and the .crt files inside my application/libraries folder. 1)我将facebook.php , base_facebook.php和.crt文件放在我的application / libraries文件夹中。
2) I created a controller to handle all Facebook API methods for my future Ajax integration. 2)我创建了一个控制器来处理我未来的Ajax集成的所有Facebook API方法。
3) I loaded the library into the constructor of my new controller. 3)我将库加载到新控制器的构造函数中。
This is the error I'm getting. 这是我得到的错误。
An Error Was Encountered
Unable to load the requested class: Facebook
My controller class looks like this. 我的控制器类看起来像这样。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Fbapi extends Base_Controller {
public function __construct()
{
parent::__construct();
}
public function index() {
$this->load->library('facebook', $this->config->item('fb_api'));
}
}
All off my library filenames are lowercase and the the class names are UCFIRST. 我的库文件名都是小写的,类名是UCFIRST。
I tried making a new library called test_library that echoed a response to the browser and this works perfectly fine so I'm really stumped as to why my Facebook libraries aren't being loaded... 我尝试创建一个名为test_library的新库,它回应了对浏览器的响应,这完全正常,所以我真的很难过为什么我的Facebook库没有被加载...
Thank you in advance. 先感谢您。
The reason it isn't being loaded is because it isn't a CodeIgniter library, so you can't import it as one. 它未加载的原因是因为它不是CodeIgniter库,因此您无法将其作为一个导入。 Instead, to access the Facebook object, you need to import the file as you would normally in PHP.
相反,要访问Facebook对象,您需要像在PHP中一样导入文件。
I would use something like this: 我会用这样的东西:
// Include the Facebook PHP SDK
require_once("facebook/facebook.php");
// Create the Facebook object
$config = array();
$config['appId'] = $this->config->item('facebook_app_id');
$config['secret'] = $this->config->item('facebook_secret');
$config['fileUpload'] = false;
$facebook = new Facebook($config);
You can then access the SDK using the $facebook
object. 然后,您可以使用
$facebook
对象访问SDK。
You'll need to amend the path in the require_once()
to point to your facebook.php
file, and you'll need to place the config settings for facebook_app_id
and facebook_secret
in your CodeIgniter config, but I've used something similar to this before and it worked fine. 您需要修改
require_once()
的路径以指向您的facebook.php
文件,并且您需要在您的CodeIgniter配置中放置facebook_app_id
和facebook_secret
的配置设置,但我使用了与此类似的东西以前它工作正常。
If you're doing a lot of work with the Facebook SDK, you might also want to consider spinning this out into a separate CodeIgniter library and writing methods for interfacing with Facebook - in my case I did that as there were several places where I needed to check if the user was logged into Facebook and turning it into a separate library made things easier. 如果您正在使用Facebook SDK进行大量工作,您可能还需要考虑将其转换为单独的CodeIgniter库并编写与Facebook连接的方法 - 在我的情况下,我这样做,因为我需要几个地方检查用户是否登录到Facebook并将其转换为单独的库使事情变得更容易。 Or you could place it in the constructor, store it as
$this->facebook
instead of $facebook
, and have access to it throughout the controller. 或者您可以将它放在构造函数中,将其存储为
$this->facebook
而不是$facebook
,并在整个控制器中访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.