skip to Main Content

I am creating a web application on Codeigniter 3.2 which works with the Facebook Graph API. In order to make GET & POST HTTP requests, I need a curl library for Codeigniter. I have found Guzzle but I Don’t know how to use Guzzle on Codeigniter.

2

Answers


  1. Check this link:

    https://github.com/rohitbh09/codeigniter-guzzle

      $this->load->library('guzzle');
    
      # guzzle client define
      $client     = new GuzzleHttpClient();
    
      #This url define speific Target for guzzle
      $url        = 'http://www.google.com';
    
      #guzzle
      try {
        # guzzle post request example with form parameter
        $response = $client->request( 'POST', 
                                       $url, 
                                      [ 'form_params' 
                                            => [ 'processId' => '2' ] 
                                      ]
                                    );
        #guzzle repose for future use
        echo $response->getStatusCode(); // 200
        echo $response->getReasonPhrase(); // OK
        echo $response->getProtocolVersion(); // 1.1
        echo $response->getBody();
      } catch (GuzzleHttpExceptionBadResponseException $e) {
        #guzzle repose for future use
        $response = $e->getResponse();
        $responseBodyAsString = $response->getBody()->getContents();
        print_r($responseBodyAsString);
      }
    
    Login or Signup to reply.
  2. You can integrate Guzzle into Codeigniter 3.x by following the following steps:

    NOTE: I was doing this on Windows, should work on other platforms too.

    1. Open the command terminal on your computer
    2. Change directory to your Codeigniter app installation (i.e. my app is called MyCodeigniterApp)

    cd C:wamp64wwwMyCodeigniterApp

    1. Install Composer in that directory by running the following command on the terminal

    curl -sS https://getcomposer.org/installer | php

    1. After installing Composer, we can now install Guzzle by running the following command on the terminal

    composer require guzzlehttp/guzzle

    if you encounter the following error while executing the above command
    [RuntimeException]
require-dev.mikey179/vfsStream is invalid, it should not contain uppercase characters. Please use mikey179/vfsstrea
m instead.

    follow the recommendation in the error message.

    Open composer.json located in your App root folder i.e. C:wamp64wwwMyCodeigniterApp

    then change

    "mikey179/vfsStream": "1.1.*"

    to

    "mikey179/vfsstream": "1.1.*"

    You can now re-run the command in step 4 to install Guzzle

    1. After successful installation of Guzzle, you now need to make the following changes to the config.php file under the application/config directory

    make the following changes under Composer auto-loading section and let the configuration be:

    $config[‘composer_autoload’] = ‘vendor/autoload.php’;

    1. The integration is complete, now you can use Guzzle in your controllers or models as below or by following the guides from Guzzle documentation on https://docs.guzzlephp.org/en/stable/

       //Create guzzle http client
       $client = new GuzzleHttpClient(); 
       $res = $client->request('GET', 'https://api.github.com/user', [
           'auth' => ['user', 'pass']
       ]);
       echo $res->getStatusCode();
       // "200"
       echo $res->getHeader('content-type')[0];
       // 'application/json; charset=utf8'
       echo $res->getBody();
       // {"type":"User"...'
      

    DONE…….

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search