ajayvohra2005/hack-http-client

Hack语言实现的HTTP客户端

v0.9.0 2021-11-05 00:58 UTC

This package is auto-updated.

Last update: 2024-09-05 07:31:39 UTC


README

概览

此项目实现了Hack语言中的HTTP客户端。

  • 简单的HTTP客户端接口
  • 同步和异步请求
  • 中间件支持

要求

HHVM 4.132及以上版本。

安装

  • 使用Git克隆此仓库

  • 安装composer

  • 在此仓库的根目录下,运行以下命令

      composer install
    

要使用此包,

    composer require ajayvohra2005/hack-http

运行测试

测试使用Node.js编写的模拟HTTP服务器。该项目使用Node.js版本v17.0.1进行了测试。安装Node.js和此项目后,在仓库根目录下运行以下命令

    ./vendor/bin/hacktest tests/

快速入门示例

use namespace HackHttp\Message as HM;
use namespace HackHttp\Client as HC;

<<__EntryPoint>>
function quick_start(): void 
{
  require_once(__DIR__.'/../vendor/autoload.hack');
  \Facebook\AutoloadMap\initialize();

  $client = new HC\Client();
  // Send a synchronous request.
  $response = $client->request('GET', 'https://docs.hhvm.com/hack/');

  echo $response->getStatusCode(); // 200
  echo $response->getHeaderLine('content-type'); // 'text/html'
  echo $response->getBody()->__toString(); // <!DOCTYPE html><html>...

  // Send an asynchronous request.
  $request = new HM\Request('GET', 'http://httpbin.org');
  $promise = $client->sendAsync($request)->then( (mixed $response): void ==> {
      if($response is HM\Response) {
        echo 'I completed! ' . $response->getBody()->__toString();
      }
  });

  $promise->wait();  
}

许可证

Hack Http包在MIT许可证(MIT)下提供。有关更多信息,请参阅许可证文件

致谢

Guzzle, PHP HTTP客户端PSR-7消息实现项目启发了此代码。