Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Requests Library within Codeigniter

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

like image 629
Edward Avatar asked Jun 24 '14 01:06

Edward


People also ask

What is request in CodeIgniter?

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.

What are libraries in CodeIgniter?

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.

Can a programmer extend the native libraries in CodeIgniter?

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.


1 Answers

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:

  1. In config.php instead of setting $config['composer_autoload'] = TRUE, you can set it to the path where the vendor file is located: $config['composer_autoload'] = '/path/to/vendor/autoload.php'
  2. Move the composer.json file to the application folder and then run composer update.

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.

like image 149
Craig Jacobs Avatar answered Oct 14 '22 01:10

Craig Jacobs