From 18f29c184f8ff067d3d651e26a123926eb5533d3 Mon Sep 17 00:00:00 2001 From: Inhere Date: Sat, 11 Nov 2023 10:26:06 +0800 Subject: [PATCH] up: update the page docs and update ci config --- .github/workflows/php.yml | 4 +- README.md | 12 ++-- README.zh-CN.md | 117 ++++++++++++++++++++++++++++++++++++++ index.html | 25 ++++++++ phpunit.xml | 33 ++++------- test/_navbar.md | 14 +++++ 6 files changed, 176 insertions(+), 29 deletions(-) create mode 100644 README.zh-CN.md create mode 100644 index.html create mode 100644 test/_navbar.md diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index eeb81e9..fe1ef13 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: true matrix: - php: [8.0, 8.1] # 7.2, 7.3, + php: [8.0, 8.1, 8.2] # # os: [ubuntu-latest] # macOS-latest, windows-latest, # include: # will not testing on php 7.2 # - os: 'ubuntu-latest' @@ -53,4 +53,4 @@ jobs: run: composer update --no-progress - name: Run test suite - run: phpunit --debug + run: phpunit diff --git a/README.md b/README.md index 4f8aec6..2a737fa 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,13 @@ [![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/phppkg/http-client)](https://github.com/phppkg/http-client) [![Github Actions Status](https://github.com/phppkg/http-client/workflows/Unit-tests/badge.svg)](https://github.com/phppkg/http-client/actions) -An easy-to-use HTTP client library for PHP. support CURL, file, fsockopen, stream drivers. +An easy-to-use HTTP client library for PHP. Support CURL, file, fsockopen, stream drivers. -- 简单易于使用 -- 可用的驱动包括: `curl` `swoole` `fsockopen` `stream` `fopen` -- 支持 `GET,POST,PATCH,PUT,HEAD,DELETE` 等请求方法 -- 支持设置代理,自定义headers -- 实现接口 [PSR 18](https://github.com/php-fig/http-client) +- Simple and easy to use HTTP client +- Support drivers: `curl` `swoole` `fsockopen` `stream` `fopen` +- Support all HTTP method. eg: `GET,POST,PATCH,PUT,HEAD,DELETE` +- Support setting proxy, customizing headers, auth, content-type etc. +- Implement interface [PSR 18](https://github.com/php-fig/http-client) ## 安装 diff --git a/README.zh-CN.md b/README.zh-CN.md new file mode 100644 index 0000000..105831b --- /dev/null +++ b/README.zh-CN.md @@ -0,0 +1,117 @@ +# HTTP Client + +[![License](https://img.shields.io/packagist/l/phppkg/http-client.svg?style=flat-square)](LICENSE) +[![Php Version](https://img.shields.io/badge/php-%3E=8.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/phppkg/http-client) +[![Latest Stable Version](http://img.shields.io/packagist/v/phppkg/http-client.svg)](https://packagist.org/packages/phppkg/http-client) +[![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/phppkg/http-client)](https://github.com/phppkg/http-client) +[![Github Actions Status](https://github.com/phppkg/http-client/workflows/Unit-tests/badge.svg)](https://github.com/phppkg/http-client/actions) + +An easy-to-use HTTP client library for PHP. Support CURL, file, fsockopen, stream drivers. + +- 简单易于使用的HTTP客户端 +- 可用的驱动包括: `curl` `swoole` `fsockopen` `stream` `fopen` +- 支持 `GET,POST,PATCH,PUT,HEAD,DELETE` 等请求方法 +- 支持设置代理,自定义headers,auth,content-type 等 +- 实现接口 [PSR 18](https://github.com/php-fig/http-client) + +## 安装 + +```bash +composer require phppkg/http-client +``` + +## 使用 + +### 创建客户端实例 + +**自动选择驱动类**: + +```php +use PhpPkg\Http\Client\Client; + +// use factory +$client = Client::factory([ + 'driver' => 'curl', // stream, fsock, fopen, file, co, co2 + + // ... 更多选项 + 'baseUrl' => 'http://my-site.com' +]); +``` + +**直接使用指定的类**: + +```php +$options = [ + 'baseUrl' => 'http://my-site.com' + // ... +]; +$client = CurlClient::create($options); +$client = FileClient::create($options); +$client = FSockClient::create($options); +$client = FOpenClient::create($options); +$client = CoClient::create($options); +``` + +### 基本使用 + +```php +use PhpPkg\Http\Client\Client; + +$client = Client::factory([]); + +$client->get('/users/1'); + +$post = ['name' => 'john']; +$client->post('/users/1', $post); + +// add ajax header +$client->byAjax()->post('/users/1', $post); + +// add json content type +$client->json('/users/1', json_encode($post)); +// or +$client->byJson()->post('/users/1', json_encode($post)); + +$statusCode = $client->getStatusCode(); +$headers = $client->getResponseHeaders(); + +// get body data +$data = $client->getResponseBody(); +$array = $client->getArrayData(); +``` + +**解析响应Body**: + +```php +$data = $client->getDataObject(); +$data->getInt('createTime', 0); + +$user = new User(); +$client->bindBodyTo($user); +vdump($user->name); +``` + +### 文件上传下载 + +- `public function upload(string $url, string $field, string $filePath, string $mimeType = '')` +- `public function download(string $url, string $saveAs)` +- `public function downloadImage(string $imgUrl, string $saveDir, string $rename = '')` + +```php +$client = CurlClient::create([ + // ... +]); + +$client->upload(...); +``` + +## 常用方法 + +- `getJsonArray/getArrayData(): array` +- `getJsonObject(): stdClass` +- `getDataObject(): DataObject` +- `bindBodyTo(object $obj): void` + +## LICENSE + +[MIT](LICENSE) diff --git a/index.html b/index.html new file mode 100644 index 0000000..6b4d25a --- /dev/null +++ b/index.html @@ -0,0 +1,25 @@ + + + + + + + + Http-client - An easy-to-use HTTP client library for PHP. support CURL, file, fsockopen, stream drivers. + + +
+ + + + \ No newline at end of file diff --git a/phpunit.xml b/phpunit.xml index 13a34d1..73cb478 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,23 +1,14 @@ - - - - - test - - - - - - src - - + + + + + test + + + + + src + + diff --git a/test/_navbar.md b/test/_navbar.md new file mode 100644 index 0000000..a58e7d3 --- /dev/null +++ b/test/_navbar.md @@ -0,0 +1,14 @@ +* [English](/README.md) +* [中文说明](/README.zh-CN.md) +* **[PHPPkg](https://github.com/phppkg)** + * [Config](https://phppkg.github.io/config/ "🗂 Config load, management, merge, get, set and more.") + * [EasyTpl](https://phppkg.github.io/easytpl/ "⚡️ Simple and fastly template engine for PHP") + * [Http-client](https://phppkg.github.io/http-client/ "An easy-to-use HTTP client library for PHP") + * [PhpGit](https://phppkg.github.io/phpgit/ "A Git wrapper library for PHP") + * [Ini](https://phppkg.github.io/ini/ "💪 An enhanced INI format parser written in PHP") + * [Jenkins-client](https://phppkg.github.io/jenkins-client/ "Designed to interact with Jenkins CI using its API") + * [Console](https://inhere.github.io/php-console/ "🖥 PHP CLI library, provide console options, arguments parse") + * [Validate](https://inhere.github.io/php-validate/ "php data validate engine") +* **[Toolkit](https://github.com/php-toolkit)** + * [PFlag](https://php-toolkit.github.io/pflag/ "console option and argument parse") + * [Stdlib](https://php-toolkit.github.io/stdlib/ "Useful basic tools library for PHP development.")