jenchik/http

从 vinelab/http 分支,根据自身需求进行了一些调整。

维护者

详细信息

github.com/jenchik/http

源代码

1.3.0 2015-06-04 14:37 UTC

This package is not auto-updated.

Last update: 2024-10-02 07:57:24 UTC


README

### 从 github.org 上的 vinelab/http 分支 fork

http://Client

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

安装

Composer

  • "jenchik/http": "dev-master" 用于安装最新版本,请参照安装说明。
// change this to point correctly according
// to your folder structure.
require './vendor/autoload.php';

use Spc\Http\Client as HttpClient;

$client = new HttpClient;

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

var_dump($response->json());

Laravel

编辑 app.php 文件,并在 'providers' 数组中添加 'Spc\Http\HttpServiceProvider',

它将自动别名自己为 HttpClient,因此无需在 app.php 中进行别名设置,除非您想自定义它 - 在这种情况下,编辑 app.php 中的 'aliases',添加 'MyHttp' => 'Spc\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();

头部

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

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

强制 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)类似的过程。支持 GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD

待办事项

  • 改进测试,包括测试所有响应方法(如 statusCode...)
  • 包括处理不良数据/错误的测试
  • 改进测试,包括对所有 HTTP 动词的测试