{"id":13700040,"url":"https://github.com/Yurunsoft/YurunHttp","last_synced_at":"2025-05-04T18:34:06.358Z","repository":{"id":41156303,"uuid":"88499870","full_name":"Yurunsoft/YurunHttp","owner":"Yurunsoft","description":"YurunHttp 是开源的 PHP HTTP 客户端，支持链式操作，简单易用。完美支持Curl、Swoole 协程。QQ群：17916227","archived":false,"fork":false,"pushed_at":"2024-01-17T02:22:16.000Z","size":553,"stargazers_count":261,"open_issues_count":1,"forks_count":56,"subscribers_count":12,"default_branch":"v5","last_synced_at":"2025-04-09T05:05:26.789Z","etag":null,"topics":["coroutine","curl","guzzle","httpclient","httplib","php","swoole","swoole-http"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Yurunsoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-04-17T10:59:29.000Z","updated_at":"2025-01-13T12:57:16.000Z","dependencies_parsed_at":"2024-06-18T11:21:30.059Z","dependency_job_id":"9b9d507a-19fc-48f6-b3af-af220273637d","html_url":"https://github.com/Yurunsoft/YurunHttp","commit_stats":{"total_commits":360,"total_committers":5,"mean_commits":72.0,"dds":"0.036111111111111094","last_synced_commit":"9528c26753d29392d19b9af69ffcfd792fcf8798"},"previous_names":[],"tags_count":104,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2FYurunHttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2FYurunHttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2FYurunHttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Yurunsoft%2FYurunHttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Yurunsoft","download_url":"https://codeload.github.com/Yurunsoft/YurunHttp/tar.gz/refs/heads/v5","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252382857,"owners_count":21739229,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coroutine","curl","guzzle","httpclient","httplib","php","swoole","swoole-http"],"created_at":"2024-08-02T20:00:47.657Z","updated_at":"2025-05-04T18:34:02.521Z","avatar_url":"https://github.com/Yurunsoft.png","language":"PHP","funding_links":[],"categories":["HTTP and WebSocket"],"sub_categories":[],"readme":"# YurunHttp\r\n\r\n[![Latest Version](https://poser.pugx.org/yurunsoft/yurun-http/v/stable)](https://packagist.org/packages/yurunsoft/yurun-http)\r\n![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/Yurunsoft/YurunHttp/ci.yml?branch=v5)\r\n[![Php Version](https://img.shields.io/badge/php-%3E=7.1-brightgreen.svg)](https://secure.php.net/)\r\n[![IMI Doc](https://img.shields.io/badge/docs-passing-green.svg)](http://doc.yurunsoft.com/YurunHttp)\r\n[![IMI License](https://img.shields.io/github/license/Yurunsoft/YurunHttp.svg)](https://github.com/Yurunsoft/YurunHttp/blob/master/LICENSE)\r\n\r\n## 简介\r\n\r\nYurunHttp，支持智能识别 Curl/Swoole 场景的高性能 Http Client。\r\n\r\n支持链式操作，简单易用。支持并发批量请求、HTTP2、WebSocket 全双工通信协议。\r\n\r\n非常适合用于开发通用 SDK 包，不必再为 Swoole 协程兼容而头疼！\r\n\r\nYurunHttp 的目标是做最好用的 PHP HTTP Client 开发包！\r\n\r\n### 特性\r\n\r\n* GET/POST/PUT/DELETE/UPDATE 等请求方式\r\n* 浏览器级别 Cookies 管理\r\n* 上传及下载\r\n* 请求头和响应头\r\n* 失败重试\r\n* 自动重定向\r\n* HTTP 代理方式请求\r\n* SSL 证书（HTTPS）\r\n* 并发批量请求\r\n* HTTP2\r\n* WebSocket\r\n* Curl \u0026 Swoole 环境智能兼容\r\n* 连接池\r\n\r\n---\r\n\r\n开发手册文档：\u003chttps://doc.yurunsoft.com/YurunHttp\u003e\r\n\r\nAPI 文档：[https://apidoc.gitee.com/yurunsoft/YurunHttp](https://apidoc.gitee.com/yurunsoft/YurunHttp)\r\n\r\n欢迎各位加入技术支持群17916227[![点击加群](https://pub.idqqimg.com/wpa/images/group.png \"点击加群\")](https://jq.qq.com/?_wv=1027\u0026k=5wXf4Zq)，如有问题可以及时解答和修复。\r\n\r\n更加欢迎各位来提交PR（[码云](https://gitee.com/yurunsoft/YurunHttp)/[Github](https://github.com/Yurunsoft/YurunHttp)），一起完善YurunHttp，让它能够更加好用。\r\n\r\n## 重大版本更新日志\r\n\r\n\u003e 每个小版本的更新日志请移步到 Release 查看\r\n\r\nv5.0.1 支持 WebSocket 压缩及指定 Opcode\r\n\r\nv5.0.0 支持 `psr/http-message` `~2.0` 版本 (PHP \u003e= 7.1)\r\n\r\nv4.3.0 新增支持连接池\r\n\r\nv4.2.0 重构 Swoole 处理器，并发请求性能大幅提升 (PHP 版本依赖降为 \u003e= 5.5)\r\n\r\nv4.1.0 实现智能识别场景，自动选择适合 Curl/Swoole 环境的处理器\r\n\r\nv4.0.0 新增支持 `Swoole` 并发批量请求 (PHP \u003e= 7.1)\r\n\r\nv3.5.0 新增支持 `Curl` 并发批量请求 (PHP \u003e= 5.5)\r\n\r\nv3.4.0 新增支持 `Http2` 全双工用法\r\n\r\nv3.3.0 新增支持 `Http2` 兼容用法\r\n\r\nv3.2.0 新增支持 `Swoole WebSocket` 客户端\r\n\r\nv3.1.0 引入浏览器级别 `Cookies` 管理\r\n\r\nv3.0.0 新增支持 `Swoole` 协程\r\n\r\nv2.0.0 黑历史，不告诉你\r\n\r\nv1.3.1 支持 `Composer`\r\n\r\nv1.0-1.3 初期版本迭代\r\n\r\n## Composer\r\n\r\n本项目可以使用composer安装，遵循psr-4自动加载规则，在你的 `composer.json` 中加入下面的内容\r\n\r\n```json\r\n{\r\n    \"require\": {\r\n        \"yurunsoft/yurun-http\": \"~5.0\"\r\n    }\r\n}\r\n```\r\n\r\n然后执行 `composer update` 安装。\r\n\r\n之后你便可以使用 `include \"vendor/autoload.php\";` 来自动加载类。（ps：不要忘了namespace）\r\n\r\n## 用法\r\n\r\n更加详细的用法请看 `examples` 目录中的示例代码\r\n\r\n### 简单调用\r\n\r\n```php\r\n\u003c?php\r\nuse Yurun\\Util\\HttpRequest;\r\n\r\n$http = new HttpRequest;\r\n\r\n// 设置 Header 4 种方法\r\n$http-\u003eheader('aaa', 'value1')\r\n     -\u003eheaders([\r\n         'bbb' =\u003e 'value2',\r\n         'ccc' =\u003e 'value3',\r\n     ])\r\n     -\u003erawHeader('ddd:value4')\r\n     -\u003erawHeaders([\r\n         'eee:value5',\r\n         'fff:value6',\r\n     ]);\r\n\r\n// 请求\r\n$response = $http-\u003eua('YurunHttp')\r\n                 -\u003eget('http://www.baidu.com');\r\n\r\necho 'html:', PHP_EOL, $response-\u003ebody();\r\n```\r\n\r\n### 并发批量请求\r\n\r\n```php\r\nuse \\Yurun\\Util\\YurunHttp\\Co\\Batch;\r\nuse \\Yurun\\Util\\HttpRequest;\r\n\r\n$result = Batch::run([\r\n    (new HttpRequest)-\u003eurl('https://www.imiphp.com'),\r\n    (new HttpRequest)-\u003eurl('https://www.yurunsoft.com'),\r\n]);\r\n\r\nvar_dump($result[0]-\u003egetHeaders(), strlen($result[0]-\u003ebody()), $result[0]-\u003egetStatusCode());\r\n\r\nvar_dump($result[1]-\u003egetHeaders(), strlen($result[1]-\u003ebody()), $result[1]-\u003egetStatusCode());\r\n```\r\n\r\n\u003e 只有 Swoole 并发请求会受到连接池限制，Curl 不受影响\r\n\r\n### Swoole 协程模式\r\n\r\n```php\r\n\u003c?php\r\nuse Yurun\\Util\\YurunHttp;\r\nuse Yurun\\Util\\HttpRequest;\r\n\r\n// Swoole 处理器必须在协程中调用\r\ngo('test');\r\n\r\nfunction test()\r\n{\r\n    $http = new HttpRequest;\r\n    $response = $http-\u003eget('http://www.baidu.com');\r\n    echo 'html:', PHP_EOL, $response-\u003ebody();\r\n}\r\n```\r\n\r\n### 连接池\r\n\r\n在 YurunHttp 中，连接池是全局的，默认不启用。\r\n\r\n每个不同的 `host`、`port`、`ssl` 都在不同的连接池中，举个例子，下面两个 url 对应的连接池不是同一个：\r\n\r\n`http://www.imiphp.com`（`host=www.imiphp.com, port=80, ssl=false`）\r\n\r\n`https://www.imiphp.com`（`host=www.imiphp.com, port=443, ssl=true`）\r\n\r\n**启用全局连接池：**\r\n\r\n```php\r\n\\Yurun\\Util\\YurunHttp\\ConnectionPool::enable();\r\n```\r\n\r\n**禁用全局连接池：**\r\n\r\n```php\r\n\\Yurun\\Util\\YurunHttp\\ConnectionPool::disable();\r\n```\r\n\r\n**写入连接池设置：**\r\n\r\n```php\r\n// 最大连接数=16个，连接数满等待超时时间（仅 Swoole 有效）=30s\r\n// url 最后不要带斜杠 /\r\n\\Yurun\\Util\\YurunHttp\\ConnectionPool::setConfig('https://imiphp.com', 16, 30);\r\n```\r\n\r\n\u003e YurunHttp 不会限制未设置的域名的连接数\r\n\r\n**特殊请求不启用连接池：**\r\n\r\n```php\r\n$http = new HttpRequest;\r\n$http-\u003econnectionPool(false);\r\n```\r\n\r\n**获取连接池对象及数据：**\r\n\r\n```php\r\nuse Yurun\\Util\\YurunHttp\\Handler\\Curl\\CurlHttpConnectionManager;\r\nuse Yurun\\Util\\YurunHttp\\Handler\\Swoole\\SwooleHttpConnectionManager;\r\n\r\n// 获取 Curl 连接池管理器，选择你所处环境对应的类，其实一般 Curl 不太需要连接池\r\n// $manager = CurlHttpConnectionManager::getInstance();\r\n\r\n// 获取 Swoole 连接池管理器，选择你所处环境对应的类\r\n$manager = SwooleHttpConnectionManager::getInstance();\r\n\r\n// 获取连接池对象集合\r\n$pool = $manager-\u003egetConnectionPool('https://imiphp.com');\r\n\r\n// 获取连接总数\r\n$pool-\u003egetCount();\r\n\r\n// 获取空闲连接总数\r\n$pool-\u003egetFree();\r\n\r\n// 获取正在使用的连接总数\r\n$pool-\u003egetUsed();\r\n\r\n// 获取连接池配置\r\n$config = $pool-\u003egetConfig();\r\n```\r\n\r\n### WebSocket Client\r\n\r\n```php\r\ngo(function(){\r\n    $url = 'ws://127.0.0.1:1234/';\r\n    $http = new HttpRequest;\r\n    // 启用压缩\r\n    // $http-\u003ewebsocketCompression(true);\r\n    $client = $http-\u003ewebsocket($url);\r\n    if(!$client-\u003eisConnected())\r\n    {\r\n        throw new \\RuntimeException('Connect failed');\r\n    }\r\n    // 文本\r\n    $client-\u003esend('data');\r\n    // 二进制\r\n    $client-\u003esend('data', \\Yurun\\Util\\YurunHttp\\WebSocket\\Opcode::BINARY);\r\n    // 更多 Opcode 预定义在：\\Yurun\\Util\\YurunHttp\\WebSocket\\Opcode\r\n    // 也支持 WebSocket 标准中的其它 Opcode\r\n    $recv = $client-\u003erecv();\r\n    var_dump('recv:', $recv);\r\n    $client-\u003eclose();\r\n});\r\n```\r\n\r\n### Http2 兼容用法\r\n\r\n```php\r\n$http = new HttpRequest;\r\n$http-\u003eprotocolVersion = '2.0'; // 这句是关键\r\n$response = $http-\u003eget('https://wiki.swoole.com/');\r\n```\r\n\r\nCurl、Swoole Handler 都支持 Http2，但需要注意的是编译时都需要带上启用 Http2 的参数。\r\n\r\n查看是否支持：\r\n\r\nCurl: `php --ri curl`\r\n\r\nSwoole: `php --ri swoole`\r\n\r\n### Http2 全双工用法\r\n\r\n\u003e 该用法仅支持 Swoole\r\n\r\n```php\r\n$uri = new Uri('https://wiki.swoole.com/');\r\n\r\n// 客户端初始化和连接\r\n$client = new \\Yurun\\Util\\YurunHttp\\Http2\\SwooleClient($uri-\u003egetHost(), Uri::getServerPort($uri), 'https' === $uri-\u003egetScheme());\r\n$client-\u003econnect();\r\n\r\n// 请求构建\r\n$httpRequest = new HttpRequest;\r\n$request = $httpRequest-\u003eheader('aaa', 'bbb')-\u003ebuildRequest($uri, [\r\n    'date'  =\u003e  $i,\r\n], 'POST', 'json');\r\n\r\nfor($i = 0; $i \u003c 10; ++$i)\r\n{\r\n    go(function() use($client, $request){\r\n        // 发送（支持在多个协程执行）\r\n        $streamId = $client-\u003esend($request);\r\n        var_dump('send:' . $streamId);\r\n\r\n        // 接收（支持在多个协程执行）\r\n        $response = $client-\u003erecv($streamId, 3);\r\n        $content = $response-\u003ebody();\r\n        var_dump($response);\r\n    });\r\n}\r\n```\r\n\r\n\u003e 具体用法请看 `examples/http2Client.php`\r\n\r\n### PSR-7 请求构建\r\n\r\n```php\r\n\u003c?php\r\nuse Yurun\\Util\\YurunHttp\\Http\\Request;\r\nuse Yurun\\Util\\YurunHttp;\r\n\r\n$url = 'http://www.baidu.com';\r\n\r\n// 构造方法定义：__construct($uri = null, array $headers = [], $body = '', $method = RequestMethod::GET, $version = '1.1', array $server = [], array $cookies = [], array $files = [])\r\n$request = new Request($url);\r\n\r\n// 发送请求并获取结果\r\n$response = YurunHttp::send($request);\r\n\r\nvar_dump($response);\r\n```\r\n\r\n---\r\n\r\n更多用法详见文档：\u003chttps://doc.yurunsoft.com/YurunHttp\u003e\r\n\r\n## 商业合作\r\n\r\n现在使用 Swoole 的项目越来越多，使用 YurunHttp 开发的代码，可以原生兼容 php-fpm 和 Swoole 两种环境。\r\n\r\nYurunHttp 相比 Guzzle 性能更强，功能更加强大！\r\n\r\n现承接使用 PHP 开发相关系统、SDK 等业务，有需要的请联系 QQ: 369124067\r\n\r\n## 捐赠\r\n\r\n\u003cimg src=\"https://raw.githubusercontent.com/Yurunsoft/YurunHttp/master/res/pay.png\"/\u003e\r\n\r\n开源不求盈利，多少都是心意，生活不易，随缘随缘……\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FYurunsoft%2FYurunHttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FYurunsoft%2FYurunHttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FYurunsoft%2FYurunHttp/lists"}