Skip to content

Commit dee68d8

Browse files
committed
Initialization.
1 parent 6f74386 commit dee68d8

22 files changed

+5119
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
indent_size = 2
7+
indent_style = space
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.php]
15+
indent_size = 4

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.buildpath
2+
.settings/
3+
.project
4+
*.patch
5+
.idea/
6+
.git/
7+
runtime/
8+
vendor/
9+
temp/
10+
*.lock

README-en.md

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# Saber
2+
3+
[![Latest Version](https://img.shields.io/github/release/swlib/swlib.svg?style=flat-square)](https://github.com/swlib/saber/releases)
4+
[![Build Status](https://travis-ci.org/swlib/saber.svg?branch=master)](https://github.com/swlib/saber/releases)
5+
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg?maxAge=2592000)](https://secure.php.net/)
6+
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=2.1.2-brightgreen.svg?maxAge=2592000)](https://github.com/swoole/swoole-src)
7+
[![Saber License](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/swlib/saber/blob/master/LICENSE)
8+
9+
## 简介
10+
11+
The PHP high-performance HTTP client for `Swoole Humanized Component Library`, based on Swoole native coroutine client, supports multiple styles of operation, provides high-performance solutions at the bottom, allows developers to focus on feature development, and emancipate from traditional synchronous blocking network libs.
12+
13+
- Development based on Swoole Coroutine Client
14+
- User-friendly style, ajax.js/axios.js/requests.py users' gospel, also supports PSR style operation
15+
- Complete browser-level cookie management mechanism, perfect for crawler/API proxy applications
16+
- Request/response interceptors
17+
- Multiple requests concurrent, concurrent redirection optimization, automated multiplexing long connections
18+
- Automatic transcoding of response messages
19+
- HTTPS connection, CA certificate automation support
20+
- HTTP/Socks5 Proxy Support
21+
- Redirection control, automated long connection multiplexing
22+
- Automation Encode Request/Parse Response Data
23+
- Milliseconds timeout timer
24+
- Random UA Generator
25+
26+
27+
28+
29+
## Requirement
30+
31+
- PHP7 or later
32+
- Swoole **2.1.2** or later
33+
34+
35+
36+
37+
## Examples
38+
39+
All of Saber's static methods have a corresponding method in the instance. The static method is implemented by a default client instance.
40+
41+
### Easy Request
42+
43+
```php
44+
go(function () {
45+
Saber::get('http://httpbin.org/get');
46+
Saber::post('http://httpbin.org/post');
47+
Saber::put('http://httpbin.org/put');
48+
Saber::patch('http://httpbin.org/patch');
49+
Saber::delete('http://httpbin.org/delete');
50+
});
51+
```
52+
53+
### Create Instance
54+
55+
API proxy service applicable
56+
57+
```php
58+
go(function () {
59+
$saber = Saber::create([
60+
'base_uri' => 'http://httpbin.org',
61+
'headers' => ['Accept-Language' => 'en,zh-CN;q=0.9,zh;q=0.8']
62+
]);
63+
$response = $saber->get('/get');
64+
echo $response;
65+
});
66+
```
67+
68+
### Multi Request
69+
Note: A concurrent redirection optimization scheme is used here. Multiple redirects are always concurrent and do not degenerate into a single request for the queue.
70+
```php
71+
go(function () {
72+
$responses = Saber::requests([
73+
['uri' => 'http://github.com/'],
74+
['uri' => 'http://github.com/'],
75+
['uri' => 'https://github.com/']
76+
]);
77+
echo "multi-requests [ {$responses->success_num} ok, {$responses->error_num} error ]:\n" .
78+
"consuming-time: {$responses->time}s\n";
79+
});
80+
// multi-requests [ 3 ok, 0 error ]:
81+
// consuming-time: 0.79090881347656s
82+
```
83+
### HTTP Proxy
84+
85+
Support HTTP and Socks5
86+
87+
```php
88+
go(function () {
89+
$uri = 'http://myip.ipip.net/';
90+
echo Saber::get($uri, ['proxy' => 'http://127.0.0.1:1087'])->body;
91+
echo Saber::get($uri, ['proxy' => 'socks5://127.0.0.1:1086'])->body;
92+
});
93+
```
94+
95+
### PSR Style
96+
97+
```php
98+
go(function () {
99+
$response = Saber::psr()
100+
->withMethod('POST')
101+
->withUri(new Uri('http://httpbin.org/post?foo=bar'))
102+
->withQueryParams(['foo' => 'option is higher-level than uri'])
103+
->withHeader('content-type', ContentType::JSON)
104+
->withBody(new BufferStream(json_encode(['foo' => 'bar'])))
105+
->exec()->recv();
106+
107+
echo $response->getBody();
108+
});
109+
```
110+
111+
112+
113+
## Install
114+
115+
**The recommended way to install Saber is through [Composer](http://getcomposer.org/)**
116+
117+
```shell
118+
composer require swlib/saber
119+
```
120+
121+
how to install composer?
122+
```bash
123+
# Install Composer
124+
curl -sS https://getcomposer.org/installer | php
125+
```
126+
```bash
127+
# Global install
128+
mv composer.phar /usr/local/bin/composer
129+
```
130+
131+
132+
After installing, you need to require Composer's autoloader:
133+
134+
```php
135+
require 'vendor/autoload.php';
136+
```
137+
138+
You can then later update Saber using composer:
139+
140+
```
141+
composer update
142+
```

README.md

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Saber
2+
3+
[![Latest Version](https://img.shields.io/github/release/swlib/swlib.svg?style=flat-square)](https://github.com/swlib/saber/releases)
4+
[![Build Status](https://travis-ci.org/swlib/saber.svg?branch=master)](https://github.com/swlib/saber/releases)
5+
[![Php Version](https://img.shields.io/badge/php-%3E=7.0-brightgreen.svg?maxAge=2592000)](https://secure.php.net/)
6+
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=2.1.2-brightgreen.svg?maxAge=2592000)](https://github.com/swoole/swoole-src)
7+
[![Saber License](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/swlib/saber/blob/master/LICENSE)
8+
9+
## 简介
10+
11+
HTTP军刀, `Swoole人性化组件库`之PHP高性能HTTP客户端, 基于Swoole原生协程, 支持多种风格操作, 底层提供高性能解决方案, 让开发者专注于功能开发, 从传统同步阻塞且配置繁琐的Curl中解放.
12+
13+
- 基于Swoole协程Client开发
14+
- 人性化使用风格, ajax.js/axios.js/requests.py用户福音, 同时支持PSR风格操作
15+
- 浏览器级别完备的Cookie管理机制, 完美适配爬虫/API代理应用
16+
- 请求/响应拦截器
17+
- 多请求并发, 并发重定向优化, 自动化复用长连接
18+
- 响应报文自动编码转换
19+
- HTTPS连接, CA证书自动化支持
20+
- HTTP/Socks5 Proxy支持
21+
- 重定向控制, 自动化长连接复用
22+
- 自动化 编码请求/解析响应 数据
23+
- 毫秒超时定时器
24+
- 随机UA生成器
25+
26+
27+
28+
29+
## 依赖
30+
31+
- PHP7 or later
32+
- Swoole **2.1.2** or later
33+
34+
35+
36+
37+
## 例子
38+
39+
Saber的所有静态方法在实例中都有对应的方法存在, 静态方法是基于一个默认的客户端实例实现的.
40+
41+
### 简单请求
42+
43+
```php
44+
go(function () {
45+
Saber::get('http://httpbin.org/get');
46+
Saber::post('http://httpbin.org/post');
47+
Saber::put('http://httpbin.org/put');
48+
Saber::patch('http://httpbin.org/patch');
49+
Saber::delete('http://httpbin.org/delete');
50+
});
51+
```
52+
53+
### 生成实例
54+
55+
适用API代理服务
56+
57+
```php
58+
go(function () {
59+
$saber = Saber::create([
60+
'base_uri' => 'http://httpbin.org',
61+
'headers' => ['Accept-Language' => 'en,zh-CN;q=0.9,zh;q=0.8']
62+
]);
63+
$response = $saber->get('/get');
64+
echo $response;
65+
});
66+
```
67+
68+
### 并发请求
69+
注意: 此处使用了并发重定向优化方案, 多个重定向总是依旧并发的而不会退化为队列的单个请求.
70+
```php
71+
go(function () {
72+
$responses = Saber::requests([
73+
['uri' => 'http://github.com/'],
74+
['uri' => 'http://github.com/'],
75+
['uri' => 'https://github.com/']
76+
]);
77+
echo "multi-requests [ {$responses->success_num} ok, {$responses->error_num} error ]:\n" .
78+
"consuming-time: {$responses->time}s\n";
79+
});
80+
// multi-requests [ 3 ok, 0 error ]:
81+
// consuming-time: 0.79090881347656s
82+
```
83+
### 网络代理
84+
85+
支持HTTP和SOCKS5代理
86+
87+
```php
88+
go(function () {
89+
$uri = 'http://myip.ipip.net/';
90+
echo Saber::get($uri, ['proxy' => 'http://127.0.0.1:1087'])->body;
91+
echo Saber::get($uri, ['proxy' => 'socks5://127.0.0.1:1086'])->body;
92+
});
93+
```
94+
95+
### PSR风格
96+
97+
```php
98+
go(function () {
99+
$response = Saber::psr()
100+
->withMethod('POST')
101+
->withUri(new Uri('http://httpbin.org/post?foo=bar'))
102+
->withQueryParams(['foo' => 'option is higher-level than uri'])
103+
->withHeader('content-type', ContentType::JSON)
104+
->withBody(new BufferStream(json_encode(['foo' => 'bar'])))
105+
->exec()->recv();
106+
107+
echo $response->getBody();
108+
});
109+
```
110+
111+
112+
113+
## 安装
114+
115+
最好的安装方法是通过 [Composer](http://getcomposer.org/) 包管理器 :
116+
117+
```bash
118+
# Install Composer
119+
curl -sS https://getcomposer.org/installer | php
120+
# Global install
121+
mv composer.phar /usr/local/bin/composer
122+
```
123+
124+
**安装Saber :**
125+
126+
```shell
127+
composer require swlib/saber
128+
```
129+
130+
安装后,你需要在项目中引入自动加载器 :
131+
132+
```php
133+
require 'vendor/autoload.php';
134+
```
135+
136+
你可以通过该命令更新 :
137+
138+
```
139+
composer update
140+
```

composer.json

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "swlib/saber",
3+
"type": "library",
4+
"keywords": [
5+
"php",
6+
"swoole",
7+
"http",
8+
"client",
9+
"psr7",
10+
"coroutine",
11+
"axios",
12+
"ajax",
13+
"requests",
14+
"curl"
15+
],
16+
"description": "Swoole coroutine HTTP client",
17+
"license": "Apache-2.0",
18+
"authors": [
19+
{
20+
"name": "twosee",
21+
"email": "[email protected]"
22+
}
23+
],
24+
"require": {
25+
"php": ">=7.0",
26+
"psr/http-message": "~1.0",
27+
"swlib/http": "dev-master",
28+
"swlib/util": "dev-master"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"Swlib\\": "src",
33+
"Swlib\\Saber\\": "src"
34+
}
35+
},
36+
"repositories": [
37+
{
38+
"type": "composer",
39+
"url": "https://packagist.phpcomposer.com"
40+
}
41+
],
42+
"require-dev": {
43+
"eaglewu/swoole-ide-helper": "dev-master",
44+
"phpunit/phpunit": "^5.7"
45+
},
46+
"scripts": {
47+
"test": "./vendor/bin/phpunit -c phpunit.xml"
48+
}
49+
}

examples/cookies.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright: Swlib
4+
* Author: Twosee <[email protected]>
5+
* Date: 2018/4/2 上午12:01
6+
*/
7+
8+
use Swlib\Saber;
9+
10+
require __DIR__ . '/../vendor/autoload.php';
11+
12+
go(function () {
13+
$cookies = Saber::get('https://github.com/')->cookies;
14+
var_dump($cookies->toRequestString()); //all cookies
15+
echo "\ncookie `_gh_sess` is discarded because its domain is `github.com` (hostonly)\n";
16+
var_dump($cookies->toRequestString('https://help.github.com/')); // this domain
17+
});

0 commit comments

Comments
 (0)