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.
You can download the zip from GitHub but recommended way by composer:
php composer.phar require guzzlehttp/guzzle
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.
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(); ?>
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
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); }
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); }
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More