PHP Guzzle HTTP Client Example With PHP 7/Laravel

This tutorial help to Understand PHP Guzzle HTTP Client with Laravel. Guzzle Client and Laravel will be used to create Rest API access. Guzzle is a popular PHP HTTP client that makes sending HTTP requests using the GET, POST, PUT, and DELETE methods simple.

We will access unfuddle api service with help of guzzle API.You can use PHP Guzzle with core PHP. There are a lot of frameworks that come with inbuilt Guzzle support. The PHP Laravel/Drupal supports PHP guzzle. To access the rest API call, do the same way as using CURL help.

PHP Guzzle HTTP Features :

  • Supports GET, HEAD, POST, DELETE, PUT, PATCH and OPTIONS HTTP methods.
  • Allows full access to request and response headers.
  • The PHP Guzzle supports persistent connections for performance improvement.
  • You can send requests in parallel.
  • Responses can be cached and served from cache.
  • Automatically requests compressed data and automatically decompresses data.
  • Supports all of the features of libcurl including authentication, redirects, SSL, proxies, etc.

How To install Guzzle HTTP Client in PHP 7

You can download the zip from GitHub but recommended way by composer:

php composer.phar require guzzlehttp/guzzle

How To Configure Guzzle HTTP Client with PHP 7

Step 1: Download zip file from here.
Step 2: Extract above zip file and copy all the files into xampp/htdocs/guzzle_test folder.
Step 3: Now run composer update command(please make sure your root folder is xampp/htdocs/guzzle_test), this command will download all dependency plugins into vendor folder.

How To Access UnFuddle API Using PHP Guzzle

We have installed and configured guzzle with php 7, Now we will verify PHP guzzle is working fine or not. Created new test.php file and putted below code into this file.

require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
 $base64 = "user:pass";
$client = new Client([
    // Base URI is used with relative requests
    'base_uri' => 'https://mydomain.unfuddle.com/api/v1',
    'timeout' => 300.0,
    'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", 'Authorization' => "Basic " . $base64],
    'http_errors' => false,
     'verify' => false
]); 
 $res = $client->request('GET', 'projects/xxx/tickets');
$response = $client->request('GET');
echo "<pre>";
echo $response->getBody();
?>

How To install Guzzle in Laravel

You can install separately with any PHP framework, I am taking Laravel 5.6 as an example framework. We will install guzzle client within laravel 5.6 and access GIT API. You need to make one entry into composer.json file:

"require": {
   "guzzlehttp/guzzle": "~6.0"
},

OR, You can install directly using the below command –
composer require guzzlehttp/guzzle:~6.0

How To Access GIT API Using HTTP Client

As we know, We are using Guzzle HTTP client to access data of GIT. The GIT is the world’s most popular distributed version control system.

private function _client() {
     $client = new Client([
          'base_uri' => 'https://gitlab.com/api/v4/',
          'timeout' => 300.0,
          'headers' => ['Content-Type' => 'application/json', "Accept" => "application/json", "PRIVATE-TOKEN" => $token],
          'http_errors' => false,
          'verify' => false
      ]);
      return $client;
   }
   /**
	 * Method to get issues by project id from gitlab
	 *
	 * @author akanksha.sagar
	 * modifed by parvez - added project name as a argument
	 */
 public function getProjects($parameters) {
   $client = $this->_client();	
   $response = $client->get("projects/")->getBody()->getContents();
		
   return json_decode($response);
		
}

How to Access POST Request Using Guzzle in Laravel

We will create POST HTTP request to post an issue into project dasbord using GIT API. We will pass the project id as a parameter.

public function postIssues($parameters) {
   $client = $this->_client();
   //$project_id = $this->project_id;	
   $response = $client->post("projects/".$parameters['projectId']."/issues", ['json' => $parameters])->getBody()->getContents();
		
   return json_decode($response);
		
}

Leave a Reply

Your email address will not be published.