dancesmile / dttp
http client for human
v1.0.0
2018-05-02 09:50 UTC
Requires
- guzzlehttp/guzzle: ^6.3
- tightenco/collect: ^5.6
Requires (Dev)
- laravel/lumen-framework: ^5.4
- phpunit/phpunit: ^7.0
This package is not auto-updated.
Last update: 2024-11-07 08:57:52 UTC
README
安装
composer require dancesmile/dttp
单个请求直接访问
<?php use Dancesmile\Dttp; // get request http://localhost:9090/get?param1=1uery1¶m2=query2 $response = Dttp::get("http://localhost:9090/get",[ "param1" => "query1", "param2" => "query2" ]); // post request http://localhost:9090/post and post www-form-urlencode data `username=username` $response = Dttp::post("http://localhost:9090/post",[ "username" => "username" ]); // delete request $response = Dttp::delete("http://localhost:9090/delete"); // put request $response = Dttp::put("http://localhost:9090/put"); // patch request $response = Dttp::patch("http://localhost:9090/patch");
单个域名多个子链接访问
<?php use Dancesmile\Dttp; $client = Dttp::client(["base_uri" => "http://localhost:9090"]); // get request http://localhost:9090/get?param1=1uery1¶m2=query2 $response = $client->get("/get",[ "param1" => "query1", "param2" => "query2" ]); // post request http://localhost:9090/post and post www-form-urlencode data `username=username` $response = $client->post("/post",[ "username" => "username" ]);
响应返回 response
<?php use Dancesmile\Dttp; $response = Dttp::post("http://localhost:9090/post",[ "username" => "username" ]); $body = $response->body(); $json = $response->json(); $content_type = $response->header("Content-Type"); $headers = $response->headers();
提交数据
<?php use Dancesmile\Dttp; Dttp::asJson()->post("http://localhost:9090/post",[ "username" => "username" ]); Dttp::asString()->post("http://localhost:9090/post","hello"); Dttp::asMultipart()->post("http://localhost:9090/post",[ [ "name" => "name", "contents" => "contens", "Content-Type" => "text/plain" ] ]); Dttp::asFormParams()->post("http://localhost:9090/post",[ "username" => "username" ]);
属性设置
<?php use Dancesmile\Dttp; $client = Dttp::asJson()->withHeaders([ "dttp-version" => 1.0 ]); $client->timeout(2); $client->accept("text/html"); $client->redirect(true) $client->verify(false) $client->contentType("application/json"); $client->post("http://localhost:9090/post",[ "username" => "username" ]);
中间件
use Dancesmile\Dttp; <!-- 前置中间件 --> Dttp::addMiddware("test",function($request, array $options)use($handler) { $this->runBeforeCallbacks($request, $options); return $handler($request, $options); })->post("http://localhost:9090/post",[ "username" => "username" ]); <!-- 后置中间件 --> Dttp::addMiddware("test",function($request, array $options)use($handler){ $promise = $handler($request, $options); return $promise->then( function (ResponseInterface $response) { return $response; } ); })->post("http://localhost:9090/post",[ "username" => "username" ]);
请求前置操作
<?php use Dancesmile\Dttp; Dttp::client("http://localhost:9090")->beforeSending(function($resquest, $option){ //user code })->get("/get")->json();