-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
up: update the page docs and update ci config
- Loading branch information
Showing
6 changed files
with
176 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |