vinelab/http

为 Laravel 框架开发的一个 http 库。别名为自己为 HttpClient

v1.5.4 2017-10-24 08:34 UTC

README

build status

Dependency Status

SensioLabsInsight

http://Client

一个智能、简单且容错的 HTTP 客户端,用于发送和接收 JSON 和 XML。

安装

Composer

composer require vinelab/http

// change this to point correctly according
// to your folder structure.
require './vendor/autoload.php';

use Vinelab\Http\Client as HttpClient;

$client = new HttpClient;

$response = $client->get('echo.jsontest.com/key/value/something/here');

var_dump($response->json());

Laravel

编辑 app.php 并将 'Vinelab\Http\HttpServiceProvider', 添加到 'providers' 数组中。

它将自动将自己别名为 HttpClient,所以不需要在 app.php 中别名为它,除非您想自定义它 - 在这种情况下,请编辑 app.php 中的 'aliases',添加 'MyHttp' => 'Vinelab\Http\Facades\Client',

使用方法

GET

简单

$response = HttpClient::get('http://example.org');

// raw content
$response->content();

带参数

$request = [
	'url' => 'http://somehost.net/something',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	]
];

$response = HttpClient::get($request);

// raw content
$response->content();

// in case of json
$response->json();

// XML
$response->xml();

POST

$request = [
	'url' => 'http://somehost.net/somewhere',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	]
];

$response = HttpClient::post($request);

// raw content
$response->content();

// in case of json
$response->json();

// XML
$response->xml();

选项

这些选项适用于所有请求。

超时

您可以通过设置 timeout 选项来指定请求失败的时间(未完成),单位为秒。

$request = [
	'url' => 'http://somehost.net/somewhere',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	],
	'timeout' => 10
];

头部

$response = HttpClient::get([
	'url' => 'http://somehost.net/somewhere',
	'headers' => ['Connection: close', 'Authorization: some-secret-here']
]);

// The full headers payload
$response->headers();

基本认证

$response = HttpClient::get([
	'url' => 'http://somehost.net/somewhere',
	'auth' => [
		'username' => 'user',
		'password' => 'pass'
	],
	'params' => [
		'var1'     => 'value1',
		'var2'   => 'value2'
	]
]);

摘要认证

$response = HttpClient::get([
	'url' => 'http://some.where.url',
	'digest' => [
		'username' => 'user',
		'password' => 'pass'
	],
	'params' => [
		'var1'     => 'value1',
		'var2'   => 'value2'
	]
]);

强制 HTTP 版本

HttpClient::get(['version' => 1.1, 'url' => 'http://some.url']);

原始内容

HttpClient::post(['url' => 'http://to.send.to', 'content' => 'Whatever content here may go!']);

自定义查询字符串

传递到 content 键的内容将被连接到 URL 后,再跟一个 ?

HttpClient::get(['url' => 'http://my.url', 'content' => 'a=b&c=d']);

与不同的 HTTP 动词类似的过程。支持 GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD

容错

容错允许在请求失败时(例如超时)重新发出请求。这在微服务的情况下很有用:当一个服务不可用并被另一个服务调用时,通过容错,请求将重新发出,希望目标服务能够恢复正常。

通过在请求中将 tolerant 标志设置为 true 来发出容错请求。另外,指定它应该等待多长时间才尝试再次请求(以秒为单位)timeUntilNextTry,以及它被视为失败之前的尝试次数(以秒为单位)triesUntilFailure

$request = [
	'url' => 'http://somehost.net/somewhere',
	'params' => [

		'id'     => '12350ME1D',
		'lang'   => 'en-us',
		'format' => 'rss_200'
	],
	'timeout' => 10
	'tolerant' => true,
	'timeUntilNextTry' => 1,
	'triesUntilFailure' => 3
];

在发生超时的情况下,将抛出 HttpClientRequestFailedException

重要提醒!注意:为了使用容错选项,您还必须指定 timeout 参数。