I'm using the requests library (http://requests.ryanmccue.info/) for PHP.
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);
}
: Class 'Requests' not found in ../application/controllers/test.php on line 34
The request class is an object-oriented representation of an HTTP request. This is meant to work for both incoming, such as a request to the application from a browser, and outgoing requests, like would be used to send a request from the application to a third-party application.
The essential part of a CodeIgniter framework is its libraries. It provides a rich set of libraries, which indirectly increase the speed of developing an application. The system library is located at system/libraries. All we need to do is to load the library that we want to use.
As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries directory.
The asker doesn't say what version of Codeigniter they are using, but for 3.x I don't think the accepted answer is the correct way to do it. CI Autoloading documentation here: https://www.codeigniter.com/user_guide/general/autoloader.html
In 3.x, first you want to turn on Composer autoloading in the application/config/config.php file:
$config['composer_autoload'] = true;
Note this comment in config.php, it may be very important. More on this below.
// Enabling this setting will tell CodeIgniter to look for a Composer
// package auto-loader script in application/vendor/autoload.php.
Then if you have this in your composer.json file:
"require": {
"rmccue/requests": ">=1.0"
},
"autoload": {
"psr-0": {"Requests": "library/"}
}
(there is probably a lot more stuff in your file, I edited it out for brevity)
Once you run composer update, you should end up with Requests working via Composer autoloading, so this should work without the need to specifically load vendor/autoload.php, which you want to avoid.
Requests::register_autoloader();
NOTE:
I have multiple installs of Codigniter on my server, and I followed the instructions as prescribed. In my case the vendor directory and composer.json were installed at the same level as the application folder, not in the application folder, therefore enabling Composer autoloading didn't work. If this happens to you, you have 2 choices:
Anyway, hope this helps anyone who might get stuck with this - I burned a good 2 hours trying to resolve the issue before I finally realized that my vendor directory wasn't where CI was looking for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With