Ads

PHP Guzzle HTTP Client Example With PHP 7/Laravel

Encrypting your link and protect the link from viruses, malware, thief, etc! Made your link safe to visit.



 This tutorial help to Understand PHP Guzzle HTTP Client with Laravel.We will create Rest API access using Guzzle Client with laravel. The Guzzle is the most popular PHP HTTP client that makes it easy to send HTTP requests Using HTTP method GET, POST, PUT and DELETE.

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 comes with inbuilt Guzzle support. The PHP Laravel/Drupal supports PHP guzzle. You can do all same thing as CURL help to access rest API call.

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 recommend 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 "";
echo $response->getBody();
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 "";
echo $response->getBody();

How To Install Guzzle In Laravel

You can install separately with any PHP framework, I am taking laravel 5.6 as 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 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 Codeslide Market
	 * modifed by Abhishek - 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);

0 Response to "PHP Guzzle HTTP Client Example With PHP 7/Laravel"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel