tmpbin/http

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

维护者

详细信息

github.com/tmpbin/http

源代码

v1.5.4 2017-06-21 11:24 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 参数。