5

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.

Poliakoff
  • 1,592
  • 1
  • 21
  • 40
Mohammad Raquib
  • 51
  • 1
  • 1
  • 6
  • 1
    My opinion is that cURL isn't that hard to use and that Guzzle (while very slick) is major overkill for your needs. Spend some time looking at the [PHP cURL manual](http://php.net/manual/en/book.curl.php) and then check SO for examples - [for example](http://stackoverflow.com/questions/15203310/how-to-use-curl-in-facebook-graph-api-request) – DFriend Oct 13 '16 at 16:28

2 Answers2

7

Check this link:

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

  $this->load->library('guzzle');

  # guzzle client define
  $client     = new GuzzleHttp\Client();

  #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 (GuzzleHttp\Exception\BadResponseException $e) {
    #guzzle repose for future use
    $response = $e->getResponse();
    $responseBodyAsString = $response->getBody()->getContents();
    print_r($responseBodyAsString);
  }
4

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:\wamp64\www\MyCodeigniterApp

  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:\wamp64\www\MyCodeigniterApp

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 \GuzzleHttp\Client(); 
     $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.......

Winter MC
  • 355
  • 2
  • 7