# Saber
[![Latest Version](https://img.shields.io/github/release/swlib/saber.svg?style=flat-square)](https://github.com/swlib/saber/releases)
[![Build Status](https://travis-ci.org/swlib/saber.svg?branch=master)](https://travis-ci.org/swlib/saber)
[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg?maxAge=2592000)](https://secure.php.net/)
[![Swoole Version](https://img.shields.io/badge/swoole-%3E=2.1.2-brightgreen.svg?maxAge=2592000)](https://github.com/swoole/swoole-src)
[![Saber License](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/swlib/saber/blob/master/LICENSE)
## 简介
HTTP军刀(呆毛王), `Swoole人性化组件库`之PHP高性能HTTP客户端, 基于Swoole原生协程, 支持多种风格操作, 底层提供高性能解决方案, 让开发者专注于功能开发, 从传统同步阻塞且配置繁琐的Curl中解放.
> **[English Document](./README-EN.md)**
- 基于Swoole协程Client开发
- 人性化使用风格, ajax.js/axios.js/requests.py用户福音, 同时支持PSR风格操作
- 浏览器级别完备的Cookie管理机制, 完美适配爬虫/API代理应用
- 请求/响应/异常拦截器
- 多请求并发, 并发重定向优化
- 连接池, 自动化复用长连接
- 通道池(Chan): 最大连接数限制+无阻塞
- HTTPS连接, CA证书自动化支持
- HTTP/Socks5 Proxy支持
- WebSocket连接支持
- 毫秒级超时定时器
- 自动化 编码请求/解析响应 数据
- 响应报文自动编码转换
- 异步超大文件上传/下载, 断点重传
- 自动重试机制
- 单次并发数控制
- 多模式/超细粒度异常处理机制
- (=)浏览器级别缓存机制
- (=)随机UA生成器
------
<br>
## 安装
最好的安装方法是通过 [Composer](http://getcomposer.org/) 包管理器 :
```shell
composer require swlib/saber
```
------
## 依赖
- **PHP71** or later
- Swoole 2.1.2 or later
- **Swoole 4 is the best**
------
<br>
## 协程调度
Swoole底层实现协程调度, **业务层无需感知**, 开发者可以无感知的**用同步的代码编写方式达到异步IO的效果和超高性能**,避免了传统异步回调所带来的离散的代码逻辑和陷入多层回调中导致代码无法维护.
需要在`onRequet`, `onReceive`, `onConnect`等事件回调函数中使用, 或是使用go关键字包裹 (`swoole.use_shortname`默认开启).
```php
go(function () {
echo SaberGM::get('http://httpbin.org/get');
})
```
------
## 目录
- <a href="#例子">例子</a>
- <a href="#静态方法">静态方法</a>
- <a href="#生成实例">生成实例</a>
- <a href="#生成会话">生成会话</a>
- <a href="#并发请求">并发请求</a>
- <a href="#数据解析">数据解析</a>
- <a href="#网络代理">网络代理</a>
- <a href="#文件上传">文件上传</a>
- <a href="#超大文件下载">超大文件下载</a>
- <a href="#自动重试">自动重试</a>
- <a href="#缓存机制">缓存机制</a>
- <a href="#psr风格">PSR风格</a>
- <a href="#websocket">WebSocket</a>
- <a href="#极限压力测试">极限压力测试</a>
- <a href="#列式请求集">列式请求集</a>
- <a href="#单次并发控制">单次并发控制</a>
- <a href="#高性能无极限协程连接池">高性能无极限协程连接池</a>
- <a href="#无限连接池">无限连接池</a>
- <a href="#定容连接池">定容连接池</a>
- <a href="#动态变容">动态变容</a>
- <a href="#注意事项">注意事项</a>
- <a href="#注册你所希望的配置">注册你所希望的配置</a>
- <a href="#注意在一次性脚本中释放连接池">注意在一次性脚本中释放连接池</a>
- <a href="#配置参数表">配置参数表</a>
- <a href="#配置参数别名">配置参数别名</a>
- <a href="#拦截器">拦截器</a>
- <a href="#cookies">Cookies</a>
- <a href="#属性">属性</a>
- <a href="#任意格式互转">任意格式互转</a>
- <a href="#域名路径和过期时限校验">域名路径和过期时限校验</a>
- <a href="#持久化存储">持久化存储</a>
- <a href="#异常机制">异常机制</a>
- <a href="#捕获例子">捕获例子</a>
- <a href="#异常报告级别控制">异常报告级别控制</a>
- <a href="#掩码表">掩码表</a>
- <a href="#异常自定义处理函数">异常自定义处理函数</a>
- <a href="#road-map">Road Map</a>
- <a href="#why-not-http2-?">Why not Http2 ?</a>
- <a href="#ide-helper">IDE Helper</a>
- <a href="#重中之重">重中之重</a>
- <a href="#附录">附录</a>
- <a href="#saber-api">Saber API</a>
- <a href="#swlibsabergm">Swlib\SaberGM</a>
- <a href="#swlibsaber">Swlib\Saber</a>
- <a href="#swlibsaberrequest">Swlib\Saber\Request</a>
- <a href="#swlibsaberresponse">Swlib\Saber\Response</a>
- <a href="#swlibsaberrequestqueue">Swlib\Saber\RequestQueue</a>
- <a href="#swlibsaberresponsemap">Swlib\Saber\ResponseMap</a>
- <a href="#swlibsaberwebsocket">Swlib\Saber\WebSocket</a>
- <a href="#swlibsaberwebsocketframe">Swlib\Saber\WebSocketFrame</a>
------
## 例子
### 静态方法
> 数据自动打包: 传入的data会自动转换成content-type所指定的类型格式
>
> 默认为`x-www-form-urlencoded`, 也支持`json`等其它格式
`SaberGM ` := `Saber Global Manager`, 如果觉得类名有点长, 可以使用`class_alias`自己取别名, 推荐服务中使用**生成实例**的方式使用, 而把`SaberGM`作为快捷方式.
```php
SaberGM::get('http://httpbin.org/get');
SaberGM::delete('http://httpbin.org/delete');
SaberGM::post('http://httpbin.org/post', ['foo' => 'bar']);
SaberGM::put('http://httpbin.org/put', ['foo' => 'bar']);
SaberGM::patch('http://httpbin.org/patch', ['foo' => 'bar']);
```
### 生成实例
适用API代理服务
```php
$saber = Saber::create([
'base_uri' => 'http://httpbin.org',
'headers' => [
'Accept-Language' => 'en,zh-CN;q=0.9,zh;q=0.8',
'Content-Type' => ContentType::JSON,
'DNT' => '1',
'User-Agent' => null
]
]);
echo $saber->get('/get');
echo $saber->delete('/delete');
echo $saber->post('/post', ['foo' => 'bar']);
echo $saber->patch('/patch', ['foo' => 'bar']);
echo $saber->put('/put', ['foo' => 'bar']);
```
### 生成会话
Session会自动保存cookie信息, 其实现是[**浏览器级别完备**](#cookies)的
```php
$session = Saber::session([
'base_uri' => 'http://httpbin.org',
'redirect' => 0
]);
$session->get('/cookies/set?foo=bar&k=v&apple=banana');
$session->get('/cookies/delete?k');
echo $session->get('/cookies')->body;
```
### 并发请求
注意: 此处使用了并发重定向优化方案, 多个重定向总是依旧并发的而不会退化为队列的单个请求
```php
$responses = SaberGM::requests([
['uri' => 'http://github.com/'],
['uri' => 'http://github.com/'],
['uri' => 'https://github.com/']
]);
echo "multi-requests [ {$responses->success_num} ok, {$responses->error_num} error ]:\n" ."consuming-time: {$responses->time}s\n";
// multi-requests [ 3 ok, 0 error ]:
// consuming-time: 0.79090881347656s
```
```php
// 别名机制可以省略参数书写参数名
$saber = Saber::create(['base_uri' => 'http://httpbin.org']);
echo $saber->requests([
['get','/get'],
['post','/post'],
['patch','/patch'],
['put','/put'],
['delete','/delete']
]);
```
### 数据解析
目前支持`json`,`xml`,`html`,`url-query`四种格式的数据快速解析
```php
[$json, $xml, $html] = SaberGM::list([
'uri' => [
'http://httpbin.org/get',
'http://www.w3school.com.cn/example/xmle/note.xml',
'http://httpbin.org/html'
]
]);
var_dump($json->getParsedJson());
var_dump($json->getParsedJsonObject());
var_dump($xml->getParsedXml());
var_dump($html->getParsedHtml()->getElementsByTagName('h1')->item(0)->textContent);
```
### 网络代理
支持HTTP�
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
正文 swoole有两个部分。 一个是PHP扩展,用C开发的,这是核心。 另一个是框架,像yii、TP、Laravel一样,是PHP代码写的。 swoole扩展本身提供了web服务器功能,可以替代php-fpm。 而如果仅仅用swoole框架,可以像其它PHP框架一样,运行在nignx、apache等web服务器中。 swoole框架像PHP框架一样,适用于web开发。 而swoole扩展是提供了更底层的服务器通信机制,可以使用UDP、TCP等协议,而不仅仅是http。 安装方式上也不一样,swoole扩展像其它PHP扩展一样安装,可以用pecl,也可以编译安装。而swoole框架用composer引入之后安装即可,或者下载源码后手动include/require。 另外,swoole框架依赖swoole扩展,是swoole扩展的应用实例。 框架-Swoole扩展-Swoole文档中心 http://wiki.swoole.com/wiki/page/p-framework.html swoole扩展是基础,基于swoole扩展,可以做出多种框架,而不仅仅是web框架。
资源推荐
资源详情
资源评论
收起资源包目录
一个很简单实用 的 swoole 框架 麻雀虽小, 五脏俱全.rar (981个子文件)
bootstrap-combined.no-icons.min.css 116KB
jquery-ui-1.8.2.custom.css 31KB
font-awesome.min.css 22KB
template.css 8KB
sen.full.min.css 5KB
manual.css 5KB
jquery.treeview.css 4KB
abstract.css 4KB
prism.css 3KB
style.css 1KB
jquery.iviewer.css 1KB
hand.cur 1KB
grab.cur 1KB
phpdoc-cache-file_5a72b4fa5efc1217f53f085c8580c79e.dat 56KB
phpdoc-cache-file_d7e34f3176a270fb8b10756dc8590e2d.dat 50KB
phpdoc-cache-file_d506ea9bbbc4f3d805844520d3ec0efe.dat 12KB
phpdoc-cache-file_9a01fc0e340da4c3f1e4a16029a63977.dat 10KB
phpdoc-cache-file_52084c644a12bbd66735afc25ad2f294.dat 10KB
phpdoc-cache-file_4f72d2f0c107658afc99a26fbca57421.dat 8KB
phpdoc-cache-file_6a7e26e5b720585996fe921b8f5e82aa.dat 6KB
phpdoc-cache-file_afd7f5a4aa79345227cbed9e5a6c292b.dat 6KB
phpdoc-cache-file_e104f0f98ac02b55bc5b1b2d130c5663.dat 6KB
phpdoc-cache-file_d6e43950e007f826e6bcc28e26acd972.dat 5KB
phpdoc-cache-file_d49175930dd68c24960885b93c9c778a.dat 5KB
phpdoc-cache-file_126e8d62fad5f1f84b49efd71ff7be2f.dat 4KB
phpdoc-cache-file_b0c00ed2ffe152e5c96db4fd655c30ab.dat 4KB
phpdoc-cache-file_0d99355092b2d1f73510a0056776f5f1.dat 3KB
phpdoc-cache-file_1feee62f4b280fabc8563202d4dcedd1.dat 3KB
phpdoc-cache-file_f2cecb88836f3631a3d8029a8c9843d9.dat 2KB
phpdoc-cache-file_2933e17ecea1416a2b622d27f73d7b76.dat 2KB
phpdoc-cache-file_b25acd18ef4244de9a139a62c57fdc65.dat 2KB
phpdoc-cache-settings.dat 92B
phpunit.xml.dist 911B
.editorconfig 212B
.editorconfig 212B
.editorconfig 212B
fontawesome-webfont.eot 71KB
phpdocumentor-clean-icons.eot 2KB
logo_small.gif 2KB
behind-the-site.gif 2KB
ui-anim_basic_16x16.gif 2KB
search.gif 204B
search.gif 152B
file.gif 110B
folder.gif 106B
bkg_top.gif 57B
.gitignore 83B
.htaccess 229B
default.html 201KB
index.html 201KB
Functionals.html 132KB
swoole_websocket_server.html 127KB
Swoole.WebSocket.Server.html 127KB
swoole_http_server.html 121KB
Swoole.Http.Server.html 121KB
swoole_server.html 118KB
Swoole.Server.html 111KB
Constants.html 93KB
swoole_process.html 56KB
swoole_client.html 54KB
swoole_http_client.html 53KB
Swoole.Process.html 53KB
errors.html 51KB
Swoole.Client.html 51KB
Swoole.Http.Client.html 50KB
swoole_table.html 46KB
Swoole.Table.html 43KB
swoole_http_response.html 39KB
Swoole.Http.Response.html 37KB
swoole_buffer.html 36KB
swoole_atomic.html 35KB
Swoole.Buffer.html 35KB
swoole_lock.html 34KB
swoole_http_request.html 34KB
Swoole.Atomic.html 34KB
swoole_redis.html 33KB
Swoole.Lock.html 33KB
Swoole.Http.Request.html 32KB
Swoole.Redis.html 32KB
Swoole.Timer.html 30KB
Swoole.WebSocket.Frame.html 28KB
swoole_server_port.html 27KB
Server.php.html 26KB
Swoole.html 20KB
Process.html 20KB
Websocket.Frame.html 20KB
Timer.html 20KB
Http.Response.html 20KB
Http.Request.html 20KB
Websocket.Server.html 20KB
Port.html 20KB
Http.Server.html 20KB
Http.Client.html 20KB
Buffer.html 20KB
Atomic.html 20KB
Redis.html 20KB
Server.html 20KB
Client.html 20KB
Lock.html 20KB
Table.html 20KB
共 981 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
野生的狒狒
- 粉丝: 1516
- 资源: 1582
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab实现文档+程序边缘计算任务卸载与资源调度的算法,是论文的源代码,具有价值.rar
- 什么是学生成绩管理系统c++以及学习学生成绩管理系统的意义
- 什么是词向量-以及学习关于了解词向量的意义
- 什么是mybatis动态sql以及学习mybatis动态sql的意义
- 华为数据治理方法论,包括:数据治理框架、数据治理组织架构、数据治理度量评估体系以及华为数据治理案例分享
- 基于matlab实现对表面肌电信号进行归一化处理,并对归一化后的图形显示 .rar
- 基于matlab实现单级倒立摆的 T-S 模型 包括 LMI 程序源码
- 图书管理系统(struts+hibernate+spring+ext).rar
- 基于matlab实现此压缩包包含语音信号处理中的语音变声代码加音频.rar
- STM32使用PWM驱动舵机并通过OLED显示
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功