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.
-
1My 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 Answers
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);
}

- 71
- 1
- 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.
- Open the command terminal on your computer
- Change directory to your Codeigniter app installation (i.e. my app is called MyCodeigniterApp)
cd C:\wamp64\www\MyCodeigniterApp
- Install Composer in that directory by running the following command on the terminal
curl -sS https://getcomposer.org/installer | php
- 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
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
- After successful installation of Guzzle, you now need to make the following changes to the
config.php
file under theapplication/config
directory
make the following changes under Composer auto-loading
section and let the configuration be:
$config['composer_autoload'] = 'vendor/autoload.php';
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.......

- 355
- 2
- 7