Codeigniter中的PHP请求库 [英] PHP Requests Library within Codeigniter
问题描述
我正在使用PHP的请求库( http://requests.ryanmccue.info/ ) 。
I'm using the requests library (http://requests.ryanmccue.info/) for PHP.
我安装了作曲家,并在 composer.json
中添加了以下Json配置:
I installed composer and added the following Json configuration in composer.json
:
{
"require": {
"rmccue/requests": ">=1.0"
},
"autoload": {
"psr-0":{"Requests" : "library/"}
}
}
因此在我的控制器中,我试图通过库运行请求,我得到:
So in my controller i'm trying to run a request through the library and I get:
public function index()
{
Requests::register_autoloader();
$headers = array('Accept' => 'application/json');
$options = array('auth' => array('user', 'pass'));
$request = Requests::get('https://api.github.com/gists', $headers, $options);
var_dump($request->status_code);
// int(200)
var_dump($request->headers['content-type']);
// string(31) "application/json; charset=utf-8"
var_dump($request->body);
}
:在../application中找不到类请求 /controllers/test.php,第34行
: Class 'Requests' not found in ../application/controllers/test.php on line 34
推荐答案
您不需要添加 autoload
块到 composer.json
,包括 require
块中的库将使用自动加载
You shouldn't need to add the autoload
block to the composer.json
, including the library in the require
block will use the autoload configuration provided by the package.
通常,您使用自动加载块来配置通过composer软件包未安装的代码的加载。
You generally use an autoload block to configure loading of code that is not installed via a composer package.
如果还没有
require 'vendor/autoload.php';
将在需要编写器库的代码之前运行的某个位置(通常在引导程序中)
somewhere that will be run before the code that needs the composer libraries (generally it's in the "bootstrap" process)
并且,您需要运行 composer install
来实际安装配置的依赖项。
And, you need to run composer install
to actually install the configured dependencies.
这篇关于Codeigniter中的PHP请求库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!