I\'m using the requests library (http://requests.ryanmccue.info/) for PHP.
I installed composer and added the following Json configuration in composer.json
I will just add some points to a well-written answer by Gopkumar Gopalan to make it work in codeigniter 3.x.
Step 1
Add the Requests library in your composer.json
Step 2
Install dependencies composer install
Step 3
Create a file PHPRequests.php
in {codeigniter_directory}/application/libraries
with this content (Note there is a change in the path of the requests library)
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once FCPATH."vendor/rmccue/requests/library/Requests.php";
class PHPRequests {
public function __construct() {
Requests::register_autoloader();
}
}
Step 4
In your controller you can use Requests by loading PHPRequests library we created in previous step.
$this->load->library('PHPRequests');
Like this test function
public function test()
{
$this->load->library('PHPRequests');
$response = Requests::get('https://github.com/timeline.json');
var_dump($response->body);
}
Hope it helps someone
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.
There is a simple way to use Requests for PHP with Codeigniter.
You may follow these few easy steps.
Step - 1
Unzip latest Requests for PHP zip and copy the contents of the library directory to {codeigniter_directory}/application/third_party/Request-{version}
for example you have Requests for PHP 1.6.0 and your CI project is in /usr/sites/www/myciproject
then copy the files to /usr/sites/www/myciproject/application/third_party/Requests-1.6.0
.
Step - 2
Create a file PHPRequests.php
in {codeigniter_directory}/application/libraries
with this content
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once APPPATH."/third_party/Requests-1.6.0/Requests.php";
class PHPRequests {
public function __construct() {
Requests::register_autoloader();
}
}
Step - 3
In your controller you can use Requests by loading PHPRequests library we created in previous step.
$this->load->library('PHPRequests');
Like this test function
public function test()
{
$this->load->library('PHPRequests');
$response = Requests::get('https://github.com/timeline.json');
var_dump($response->body);
}
Hope this will help.
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.
You generally use an autoload block to configure loading of code that is not installed via a composer package.
You also need to require
the composer autoload file, if you haven't already by
require 'vendor/autoload.php';
somewhere that will be run before the code that needs the composer libraries (generally it's in the "bootstrap" process)
And, you need to run composer install
to actually install the configured dependencies.
HTTPFUL is the best library after I have been through several http mechanisms (some of my own :) ), i found this one is very useful. http://phphttpclient.com/
and it includes
Readable HTTP Method Support (GET, PUT, POST, DELETE, HEAD, and OPTIONS)
Custom Headers
Automatic "Smart" Parsing
Automatic Payload Serialization
Basic Auth
Client Side Certificate Auth
Request "Templates"