2017 © Pedro Peláez
 

library async-http

image

xd/async-http

  • Monday, December 11, 2017
  • by ganxiangdong
  • Repository
  • 1 Watchers
  • 1 Stars
  • 5 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

AsyncHttp is a very simple and agile HTTP asynchrony request library., (*1)

Install


composer require xd/async-http, (*2)

Examples


  1. GET:, (*3)

    //requsting 
    $req = AsyncHttp::get("http://192.168.88.2?sleepTime=3")->request();
    
    //do somthing...
    
    //get response
    $response = $req->getResponse();
    //get body
    $resBody = $response->body;
    //get http status code
    $httpStatusCode = $response->statusCode;
    //get response all header
    $headers = $response->headers;
    //get one header
    $header = $response->getHeader('Server');
    
  2. POST, (*4)

    //requesting by x-www-form-urlencode
    $postData = ['sleepTime' => 3];
    $req = AsyncHttp::post("http://192.168.88.2", $postData)->request();
    //requesting by raw JSON
    $postData = json_encode(['sleepTime' => 1]);
    $req2 = AsyncHttp::post("http://192.168.88.2", $postData)->request();
    //requesting by raw XML
    $req2 = AsyncHttp::post("http://192.168.88.2", $xmlDataStr)
     ->addHeader("Content-Type":"application/xml")->request();
    
    //do somthing...
    
    //get response
    $body1 = $req->getResponse()->body;
    $body2 = $req2->getResponse()->body; 
    $body3 = $req3->getResponse()->body; 
    
  3. PUT, (*5)

    //requesting by x-www-form-urlencode
    $putData = json_encode(['sleepTime' => 3]);
    $req = AsyncHttp::put("http://192.168.88.2/index.php", $putData)->request();
    //requesting by raw JSON
    $putData = json_encode(['sleepTime' => 1]);
    $req2 = AsyncHttp::put("http://192.168.88.2/index.php", $postData)->request();
    
    //do somthing...
    
    //get response
    $body1 = $req->getResponse()->body;
    $body2 = $req2->getResponse()->body;
    
  4. DELETE, (*6)

    //requesting
    $req = AsyncHttp::delete("http://192.168.88.2/index.php?sleepTime=3")->request();
    //requesting
    $req2 = AsyncHttp::delete("http://192.168.88.2/index.php?sleepTime=0")->request();
    
    //do somthing...
    
    //get response
    $body1 = $req->getResponse()->body;
    $body2 = $req2->getResponse()->body;
    

Other usage


  1. Custom header, (*7)

    //example 1
    $req = AsyncHttp::get("http://192.168.88.2?sleepTime=3");
    $req->addHeader("Test", 1);
    $req->request();
    
    //example 2
    $req = AsyncHttp::get("http://192.168.88.2?sleepTime=3")->addHeader("Test", 1)->addHeader("Test-X", "2")->request();
    
    //example 3
    $req = AsyncHttp::get("http://192.168.88.2?sleepTime=3");
    $req->requestHeaders = ["Test" => "1","Test-X" => "2"];
    $req->request();
    
  2. Set timeout(default 8 second), (*8)

    //example 1
    $req = AsyncHttp::get("http://192.168.88.2?sleepTime=3");
    $req->setTimeout(8);
    $req->request();
    
    //example 2
    $req = AsyncHttp::get("http://192.168.88.2?sleepTime=3")->setTimeout(8)->request();
    
    //example 3
    $req = AsyncHttp::get("http://192.168.88.2?sleepTime=3");
    $req->setTimeout = 8;
    $req->request();
    
  3. How to judge timeout or network unreachable, (*9)

    if ($response->getStatusCode() == 0) {
    
    } 
    

    ​, (*10)

The Versions

11/12 2017

dev-master

9999999-dev https://github.com/ganxiangdong/async-http-client

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-curl *

 

by xiangdonggan

async http client asynchronism asynchttp asynchttpclient

11/12 2017

1.0

1.0.0.0 https://github.com/ganxiangdong/async-http-client

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-curl *

 

by xiangdonggan

async http client asynchronism asynchttp asynchttpclient

11/12 2017

v1.0.1

1.0.1.0 https://github.com/ganxiangdong/async-http-client

  Sources   Download

MIT

The Requires

  • php >=5.5.0
  • ext-curl *

 

by xiangdonggan

async http client asynchronism asynchttp asynchttpclient