quibax / http
基于 vinelab/http 改编,根据自身需求进行了一些调整。
v1.2.0
2015-02-26 17:08 UTC
Requires
- php: >=5.4
- illuminate/support: *
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: 4.3.*
README
http://客户端
一个智能简单的HTTP客户端,用于发送和接收JSON和XML。
安装
Composer
"vinelab/http": "dev-master"或参考 packagist.org上的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 中进行别名设置,除非您想进行自定义 - 在这种情况下,编辑您的 'aliases' 在 app.php 中添加 '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();
头部
$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
待办事项
- 改进测试,包括测试所有响应方法(如statusCode...)
- 包括处理错误数据的测试
- 改进测试,包括测试所有HTTP动词