swnck / pure-request
一个简洁的PHP库,用于轻松进行HTTP请求,简化网络通信,无需直接使用cURL的复杂性。
1.0.1
2024-03-19 18:39 UTC
Requires
- ext-curl: *
README
⚠️ 开发中 ⚠️
简介
PureRequest
是一个简洁的PHP库,旨在通过提供一个无需烦恼的HTTP请求接口来简化网络通信,消除直接使用cURL的复杂性。该库支持广泛的HTTP操作,包括GET和POST请求,并高度可配置以满足各种Web应用的需求。
功能
- 易于使用的HTTP GET和POST请求界面
- 支持自定义头和正文内容
- 可配置的请求选项,包括超时和重定向处理
- 内置响应数据处理和状态码处理支持
- 安全且高效的实现
安装
要在您的机器上安装PureRequest,您需要安装Composer。在您的项目目录中运行以下命令
composer require swnck/pure-request
使用
请求配置
$request = new PureRequest((new RequestConfiguration()) ->setReturnTransfer(true) // Return the transfer as a string of the return value of curl_exec() instead of outputting it out directly ->setFollowLocation(true) // Follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set) ->setConnectTimeout(10) // The maximum number of seconds to allow cURL functions to execute );
或者如果您想保持默认配置
$request = new PureRequest();
发送GET请求
$request->get(HeaderContent::empty(), function (ResponseFrame $response) { echo $response->getContent(); echo $response->getStatusCode(); }, "https://example.com/api/data");
发送POST请求
$request->post(HeaderContent::paste(["Content-Type" => ContentType::APPLICATION_JSON, "Connection" => "keep-alive"]), BodyContent::paste([ "email" => "user@example.com", "password" => "your_password" ]), function (ResponseFrame $response) { echo $response->getContent(); }, "https://example.com/api/login");
功能
- 易于使用的HTTP GET和POST请求界面
- 支持自定义头和正文内容
- 支持PUT、DELETE、PATCH、OPTIONS、HEAD和其他HTTP方法
- 支持文件上传和multipart表单数据
- 可配置的请求选项,包括超时和重定向处理
- 内置响应数据处理和状态码处理支持
- 安全且高效的实现
- 支持cookie和会话管理
- 支持异步请求和并行处理
贡献
我们欢迎社区贡献!如果您想为PureRequest做出贡献,请将仓库分叉并提交带有您建议的更改或改进的pull request。
许可
本存储库的内容受Apache License,版本2.0的许可。