spacetab-io/harmony-http-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spacetab-io/harmony-http-client

Composer 安装命令:

composer require spacetab-io/harmony-http-client

包简介

Asynchronous http client

README 文档

README

Latest Stable Version Build Status Build status Coverage Status License

Async caching aware http client

Requirements

  • PHP 7.3+
  • Redis (if wanting to use the Redis caching provider)

In addition for non-blocking context one of the following event libraries should be installed:

Installation

composer require harmonyio/http-client

Usage

Caching requests

The following example shows how to make a request and cache the result so consecutive calls will used the cached results instead of making a new external HTTP call.

<?php declare(strict_types=1);

namespace Foo;

use Amp\Http\Client\HttpClientBuilder;
use HarmonyIO\Cache\Provider\InMemory;
use HarmonyIO\Cache\Ttl;
use HarmonyIO\HttpClient\Client\HttpClient;
use HarmonyIO\HttpClient\Message\CachingRequest;
use function Amp\Promise\wait;

// create instance of the HTTP client
$httpClient = new HttpClient(
    HttpClientBuilder::buildDefault(),
    new InMemory()
);

// create a new request to be cached for 10 seconds
$request = new CachingRequest('TestRequestKey', 'https://httpbin.org/get', 'GET', new Ttl(10));

// make the request and cache it
$result = wait($httpClient->request($request));

// all consecutive requests will now used the cached result instead of calling the external service again
$result = wait($httpClient->request($request));

Non-caching requests

It is also possible to make non-caching requests.

<?php declare(strict_types=1);

namespace Foo;

use Amp\Http\Client\HttpClientBuilder;
use HarmonyIO\Cache\Provider\InMemory;
use HarmonyIO\HttpClient\Client\HttpClient;
use Amp\Http\Client\Request;
use function Amp\Promise\wait;

// create instance of the HTTP client
$httpClient = new HttpClient(
    HttpClientBuilder::buildDefault(),
    new InMemory()
);

// create a new request to be cached for 10 seconds
$request = new Request('https://httpbin.org/get');

// make the request (the results will NOT be cache)
$result = wait($httpClient->request($request));

// make the same request again
$result = wait($httpClient->request($request));

HarmonyIO\HttpClient\Message\CachingRequest

The constructor of the caching request class expects at least a key, a TTL, the URL to make the request to and optionally an HTTP method (defaults to GET).

Optional request parts can be set in setter method of the request class as defined in the previous section.

<?php declare(strict_types=1);

namespace Foo;

use HarmonyIO\HttpClient\Message\CachingRequest;

$cachingRequest = new CachingRequest('UniqueCachingKey', 'https://httpbin.org/get');
$request = $cachingRequest->getRequest();
$request->setProtocolVersions(['1.1', '2.0']);
$request->addHeader('foo', 'bar');
$request->addHeader('baz', 'qux');

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 1
  • 推荐数: 1

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固