Skip to content

Commit

Permalink
up: update the page docs and update ci config
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 11, 2023
1 parent 21707ef commit 18f29c1
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -53,4 +53,4 @@ jobs:
run: composer update --no-progress

- name: Run test suite
run: phpunit --debug
run: phpunit
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

## 安装

Expand Down
117 changes: 117 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -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)
25 changes: 25 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<html lang="">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="UTF-8">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify/themes/vue.css">
<title>Http-client - An easy-to-use HTTP client library for PHP. support CURL, file, fsockopen, stream drivers.</title>
</head>
<body>
<div id="app"></div>
<script>
window.$docsify = {
repo: 'phppkg/http-client',
maxLevel: 3,
// 加载 _navbar.md
// loadNavbar: true,
loadNavbar: 'test/_navbar.md',
// 加载 _sidebar.md
// loadSidebar: true,
}
</script>
<script src="//cdn.jsdelivr.net/npm/docsify/lib/docsify.min.js"></script>
</body>
</html>
33 changes: 12 additions & 21 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="test/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
>
<testsuites>
<testsuite name="phppkg http-client Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="test/bootstrap.php" colors="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="phppkg http-client Test Suite">
<directory>test</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
14 changes: 14 additions & 0 deletions test/_navbar.md
Original file line number Diff line number Diff line change
@@ -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.")

0 comments on commit 18f29c1

Please sign in to comment.