Giter VIP home page Giter VIP logo

dttp's Introduction

Http client for human

Build Status License Latest Unstable Version Latest Stable Version

安装

composer require dancesmile/dttp

单个请求直接访问

<?php 
use Dancesmile\Dttp;

// get request  http://localhost:9090/get?param1=1uery1&param2=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&param2=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();

dttp's People

Contributors

dancesmile avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.