in CodeIgniter, Coding, PHP

PHP Requests Library within CodeIgniter


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/myproject then copy the files to /usr/sites/www/myproject/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);
}