Guzzle: PHP HTTP Client

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.

  • Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc…
  • Can send both synchronous and asynchronous requests using the same interface.
  • Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR-7 compatible libraries with Guzzle.
  • Abstracts away the underlying HTTP transport, allowing you to write environment and transport agnostic code; i.e., no hard dependency on cURL, PHP streams, sockets, or non-blocking event loops.
  • Middleware system allows you to augment and compose client behavior.

 

Installation:
First, install Guzzle using the composer or download from https://github.com/guzzle/guzzle

Install Guzzle through composer:
composer require guzzlehttp/guzzle

Example:
Below is the example to use Guzzle in core PHP.

<?php 
require_once 'vendor/autoload.php';

// use Application\Exception\PostcodesException;
// use Application\Util\Postcodes;
use GuzzleHttp\Client;
// use PHPUnit\Framework\TestCase;


class GuzzleApi{

    public function pushFile(){


        $client = new Client(
            ['headers' => [
                'Authorization' => 'Bearer '.$token
            ]]
        );

        $filename = 'simple.xml';
        // specify your file path
        $imagePath = '/var/www/html/test/api/guzzle/simple.xml';

        $myfile = fopen($imagePath, "r");
        $fileOpen = fread($myfile,filesize($imagePath));
        fclose($myfile);

        $response = $client->request(
            'POST',
            $this->generateApiUrl('api/project/push'),
            [
                'multipart' => [

                    [
                        'name'     => 'file[]',
                        'contents' => $fileOpen,
                        'filename' => $filename,
                    ],
                   /* [
                        'name'     => 'file[]',
                        'contents' => $fileOpen,
                        'filename' => $filename,
                    ],*/
                    [
                        'name'=>'file[location]',
                        'contents'=> $imagePath
                    ],
                    [
                        'name'=>'client_id',
                        'contents'=> '*'
                    ],
                    [
                        'name'=>'client_secret',
                        'contents'=> '**************'
                    ],
                     [
                         'name'=>'project_key',
                         'contents'=> '**************'
                     ]



                ]
            ]
        );
        
        $response = json_decode($response->getBody(),true);

        print_r($response);exit;

        //echo $response->getBody();
        
    }

    protected function authenticate()
    {
        $client = new Client();

        $url = $this->generateApiUrl('api/key/validations');
        //echo $url;exit;
        $response = $client->post($url,
            [
                'form_params' => [
                   "client_id" => '*',
                   "client_secret" => '************************',
                   "project_key" => '************************'
                ]
            ]
        );

        $response = json_decode($response->getBody());
        return $response->access_token;

    }

    function generateApiUrl($option = '/')
    {
        $baseUrl = 'https://plugin.tilkor.com/';
        return $baseUrl . $option;
    }

}



$obj = new GuzzleApi();
$obj->pushFile();

?>

Magento2 e-commerce:
Guzzle is inbuild in Magento2 e-commerce. So you don’t need to install Guzzle additionally.

You should use in the class:
use GuzzleHttp\Client;

Then create an object where you want to call.

Example:

<?php $client = new Client(); $url = 'https://plugin.tilkor.com/api/key/validations'; $response = $client->post($url,
    [
        'form_params' => [
           "client_id" => '******',
           "client_secret" => '*************************',
           "project_key" => '***************************'
        ]
    ]
);
$response = json_decode($response->getBody());
$access_token = $response->access_token;
?>

Reference:
http://docs.guzzlephp.org/en/stable/
https://github.com/guzzle/guzzle