weezqyd/http-adapters

抽象各种Http客户端的统一API

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

This package is auto-updated.

Last update: 2024-09-13 21:53:03 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

安装

此库可在Packagist上找到。推荐通过composer安装。

运行以下命令以安装包

$ composer require weezqyd/http-adapters

然后您需要安装以下之一

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

GuzzleHttp 适配器

如果您使用Guzzle,只需将选项数组传递给Http\Adapter\GuzzleHttpAdapter构造函数。请参阅Guzzle 文档以获取所有可能的选项列表。

示例

<?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"
}
 */