• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Vinelab/http: A smart, simple and fault-tolerant HTTP client for sending and rec ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Vinelab/http

开源软件地址:

https://github.com/Vinelab/http

开源编程语言:

PHP 100.0%

开源软件介绍:

build status

Dependency Status

SensioLabsInsight

http://Client

A smart, simple and fault-tolerant HTTP client for sending and recieving JSON and XML.

Installation

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

Edit app.php and add 'Vinelab\Http\HttpServiceProvider', to the 'providers' array.

It will automatically alias itself as HttpClient so no need to alias it in your app.php, unless you would like to customize it - in that case edit your 'aliases' in app.php adding 'MyHttp' => 'Vinelab\Http\Facades\Client',

Usage

GET

Simple

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

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

With Params

$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();

Options

These options work with all requests.

Timeout

You can set the timeout option in order to specify the number of seconds that your request will fail, if not completed.

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

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

Headers

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

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

Basic Auth

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

Digest Auth

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

Enforce HTTP Version

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

Raw Content

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

Custom Query String

The content passed in the content key will be concatenated to the URL followed by a ?

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

It is pretty much the same process with different HTTP Verbs. Supports GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD

Fault Tolerance

Fault tolerance allows the request to be re-issued when it fails (i.e. timeout). This is useful in cases such as Microservices: When a service is down and is being called by another service, with fault tolerance the request will be re-issued in the hopes of the destination service being up again.

Issue a fault-tolerant request by setting the tolerant flag to true in the request. Also, specify the time it should wait until it tries again with timeUntilNextTry (in seconds) and the number of tries before it is considered a failure with triesUntilFailure (in seconds).

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

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

In case of timeout occurance, a HttpClientRequestFailedException will be thrown.

IMPORTANT! Notice: In order to make use of fault tolerance option, you must specify the timeout parameter too.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap