weezqyd/http-adapters

Unified API for abstracting Various Http clients

v0.0.1 2017-07-10 22:30 UTC

This package is auto-updated.

Last update: 2022-11-13 18:07:13 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Installation

This library can be found on Packagist. The recommended way to install this is through composer.

Run these commands to install the packages:

$ composer require weezqyd/http-adapters

You then need to install one of the following:

$ composer require kriswallsmith/buzz:~0.10
$ composer require guzzlehttp/guzzle:~5.0
$ composer require guzzlehttp/guzzle:~6.0

GuzzleHttp Adapter

If you use Guzzle, just pass an array of options to the constructor of Http\Adapter\GuzzleHttpAdapter. Please refer to Guzzle Documentation. for a full list of possible options

Example

<?php

require 'vendor/autoload.php';

use Http\Adapter\GuzzleHttpAdapter;

$options = [
	    // Base URI is used with relative requests
	    'base_uri' => 'http://httpbin.org',
	    // You can set any number of default request options.
	    'timeout'  => 2.0,
	    // Pass your custom headers
	    'headers' => [
	    	'Authorization' => 'your access token',
	    	'X-Foo' => 'Bar'
	    ]
	];
// create an adapter with the options
$adapter = new GuzzleHttpAdapter($options);

// make a get request with the adapter
$response = $adapter->get('get?adappter=GuzzleAdapter');
var_dump($response);
/*
{
	"args": {
		 "adappter": "GuzzleAdapter"
	}, 
	"headers": {
		 "headers": {
			 "Authorization": "your access token",
			 "X-Foo": "Bar",
			 "Connection": "close", 
			 "Host": "httpbin.org", 
			 "User-Agent": "GuzzleHttp/6.2.1 curl/7.47.0 PHP/7.1.6"
		}, 
	"origin": "0.0.0.0", 
	"url": "https://httpbin.org/get?adappter=GuzzleAdapter"
}
 */