19 October 2016

Tutorial - Simple Way to use Guzzle in Laravel

Overview

Simple Way to use Guzzle in Laravel. 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.

Requirement

  1. PHP 5.5.0
  2. To use the PHP stream handler, allow_url_fopen must be enabled in your system's php.ini.
  3. To use the cURL handler, you must have a recent version of cURL >= 7.19.4 compiled with OpenSSL and zlib.
Note
Guzzle no longer requires cURL in order to send HTTP requests. Guzzle will use the PHP stream wrapper to send HTTP requests if cURL is not installed. Alternatively, you can provide your own HTTP handler used to send requests.

Installation

composer require "guzzlehttp/guzzle": "4.0"
in your controller
use GuzzleHttp\Client;
in your function
public function get_fromUrl(){
$client = new Client;
$response = $client->get('https://yoururl');
}
or you can use post method.
$r = $client->post('https://yoururl', [
                'verify'=>false,
                'body' => $arraydata]);
if you have trouble in ssl error. You can use verify.
// Use the system's CA bundle (this is the default setting)
$client->request('GET', '/', ['verify' => true]);

// Use a custom SSL certificate on disk.
$client->request('GET', '/', ['verify' => '/path/to/cert.pem']);

// Disable validation entirely (don't do this!).
$client->request('GET', '/', ['verify' => false]);

you can find full docs in here.

No comments: