linslin / yii2-curl
适用于 Yii2 的易于使用且功能齐全的 cURL 扩展,支持 RESTful
1.5.0
2021-02-20 10:10 UTC
Requires
- php: >=7.2.0 <9.0
- ext-curl: *
- ext-json: *
- yiisoft/yii2: ^2.0.0
Requires (Dev)
- codeception/module-asserts: ^1.3
- guzzlehttp/guzzle: >=4.1.4 <7.0
- mcustiel/phiremock: ^2.1
- mcustiel/phiremock-codeception-extension: ^2.1
- mcustiel/phiremock-codeception-module: ^1.1.2
- dev-master
- 1.5.0
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- dev-dependabot/composer/react/http-1.9.0
- dev-dependabot/composer/laminas/laminas-diactoros-2.14.0
- dev-dependabot/composer/guzzlehttp/psr7-1.9.1
- dev-dependabot/composer/guzzlehttp/guzzle-6.5.8
- dev-develop
This package is auto-updated.
Last update: 2024-09-17 20:27:58 UTC
README
为 Yii2 提供易于使用的 cURL 扩展,包括 RESTful 支持
- POST
- GET
- HEAD
- PUT
- PATCH
- DELETE
- OPTIONS
需求
- Yii2
- PHP >=7.2.0 || 8.0.1
- 安装 ext-curl, ext-json 和 php-curl
安装
推荐通过 composer 安装此扩展。
composer require --prefer-dist linslin/yii2-curl "*"
使用
扩展安装后,只需在您的代码中使用它。以下示例展示了如何处理简单的 GET 请求。
use linslin\yii2\curl; $curl = new curl\Curl(); //get http://example.com/ $response = $curl->get('http://example.com/'); if ($curl->errorCode === null) { echo $response; } else { // List of curl error codes here https://curl.haxx.se/libcurl/c/libcurl-errors.html switch ($curl->errorCode) { case 6: //host unknown example break; } }
// GET request with GET params // http://example.com/?key=value&scondKey=secondValue $curl = new curl\Curl(); $response = $curl->setGetParams([ 'key' => 'value', 'secondKey' => 'secondValue' ]) ->get('http://example.com/');
// POST URL form-urlencoded $curl = new curl\Curl(); $response = $curl->setPostParams([ 'key' => 'value', 'secondKey' => 'secondValue' ]) ->post('http://example.com/');
// POST RAW JSON $curl = new curl\Curl(); $response = $curl->setRawPostData( json_encode[ 'key' => 'value', 'secondKey' => 'secondValue' ]) ->post('http://example.com/');
// POST RAW JSON and auto decode JSON respawn by setting raw = true. // This is usefull if you expect an JSON response and want to autoparse it. $curl = new curl\Curl(); $response = $curl->setRawPostData( json_encode[ 'key' => 'value', 'secondKey' => 'secondValue' ]) ->post('http://example.com/', true); // JSON decoded response by parsing raw = true in to ->post(). var_dump($response);
// POST RAW XML $curl = new curl\Curl(); $response = $curl->setRawPostData('<?xml version="1.0" encoding="UTF-8"?><someNode>Test</someNode>') ->post('http://example.com/');
// POST with special headers $curl = new curl\Curl(); $response = $curl->setPostParams([ 'key' => 'value', 'secondKey' => 'secondValue' ]) ->setHeaders([ 'Custom-Header' => 'user-b' ]) ->post('http://example.com/');
// POST JSON with body string & special headers $curl = new curl\Curl(); $params = [ 'key' => 'value', 'secondKey' => 'secondValue' ]; $response = $curl->setRequestBody(json_encode($params)) ->setHeaders([ 'Content-Type' => 'application/json', 'Content-Length' => strlen(json_encode($params)) ]) ->post('http://example.com/');
// Avanced POST request with curl options & error handling $curl = new curl\Curl(); $params = [ 'key' => 'value', 'secondKey' => 'secondValue' ]; $response = $curl->setRequestBody(json_encode($params)) ->setOption(CURLOPT_ENCODING, 'gzip') ->post('http://example.com/'); // List of status codes here http://en.wikipedia.org/wiki/List_of_HTTP_status_codes switch ($curl->responseCode) { case 'timeout': //timeout error logic here break; case 200: //success logic here break; case 404: //404 Error logic here break; } //list response headers var_dump($curl->responseHeaders);
测试
- 在仓库根目录中运行
vendor/bin/codecept run
来运行 codeception 测试。使用 clover 报告运行 codeception:XDEBUG_MODE=coverage vendor/bin/codecept run --coverage-xml ./../../build/logs/clover.xml
。在 Windows 上运行vendor\bin\codecept.bat run
。在 Windows 上使用 clover 报告运行vendor\bin\codecept.bat run --coverage-xml ./../../build/logs/clover.xml
。
更新日志
发布 1.5.0 - 更新日志
发布 1.4.0 - 更新日志
- 添加了对 HTTP 方法 OPTIONS 的支持。
- 移除了对旧 PHP 版本的支持。最低 PHP 版本现在是 7.2.0。如果您需要 PHP 7.1.0+ 支持,请使用版本 "linslin/yii2-curl 1.3.0" - https://github.com/linslin/Yii2-Curl/releases/tag/1.3.0。
发布 1.3.0 - 更新日志
- 修复了 PATCH 请求上 HTTP 方法的解析。
- 更新了 DocBlocks 和代码重构。
- 移除了对旧 PHP 版本的支持。最低 PHP 版本现在是 7.1.3。如果您需要 PHP 5.4+ 支持,请使用版本 "linslin/yii2-curl 1.2.1" - https://github.com/linslin/Yii2-Curl/releases/tag/1.2.1。
发布 1.2.2 - 更新日志
- 将一些新的 cURL 示例添加到 readme.md。
发布 1.2.1 - 更新日志
- 添加了
setRawPostData([mixed]) [this]
,允许您以任何数据格式发送数据。
发布 1.2.0 - 更新日志
- 添加了
unsetHeader([string header]) [this]
助手,允许您取消一个特定头部的设置。 - 添加了
setHeader([string header, string value]) [this]
助手,允许您设置一个特定头部。 - 添加了
getRequestHeaders() [array]
助手,返回所有请求头部作为一个数组。 - 添加了
getRequestHeader([string headerKey]) [string|null]
助手,返回一个特定的请求头部作为字符串。 - 为
getRequestHeaders()
和getRequestHeader()
添加了新的测试用例。 - 调整了 README。
发布 1.1.3 - 更新日志
- 修复了与 patch 请求相关的问题。
- 完全添加了功能测试以实现 100% 覆盖率。
发布 1.1.2 - 更新日志
发布 1.1.1 - 更新日志
- 修复了
_httpRequest()
中参数解析错误(感谢 yemexx1) - 添加了 JSON 解码函数测试(感谢 yemexx1)
发布 1.1.0 - 更新日志
- 添加了
setHeaders() [数组]
辅助函数。 - 添加了
setPostParams() [数组]
辅助函数。 - 添加了
setGetParams() [数组]
辅助函数。 - 添加了
setRequestBody() [字符串]
辅助函数。 - 添加了
getUrl()
辅助函数。 - 添加了 API 属性
errorText [字符串|null]
- 存储描述给定错误码的字符串 - #49。 - 添加了 functionTests 确保稳定性。
- 允许 PHP 类特性 - #52。
- 修复了 header explode - #51。
版本 1.0.11 - 更新日志
- 添加了 API 属性
responseHeaders [数组|null]
,它返回所有响应头部的数组。 - 将
_defaultOptions[CURLOPT_HEADER]
更改为true
。 - 仅在常量
YII_DEBUG
为true
时启用调试配置。
版本 1.0.10 - 更新日志
- 修复了 PHP 警告 #39。
版本 1.0.9 - 更新日志
- 添加了 API 属性
responseCode [字符串|null]
,它存储 HTTP 响应代码。 - 添加了 API 属性
responseCharset [字符串|null]
,它存储响应字符集。 - 添加了 API 属性
responseLength [整数|null]
,它存储响应长度。 - 添加了 API 属性
errorCode
,它存储类似这里描述的整数错误代码:[a href="https://curl.haxx.se/libcurl/c/libcurl-errors.html" rel="nofollow noindex noopener external ugc">https://curl.haxx.se/libcurl/c/libcurl-errors.html。 - 修复了问题 https://github.com/linslin/Yii2-Curl/issues//36。
- 修复了问题 https://github.com/linslin/Yii2-Curl/issues//37 并移除了 curl 失败时的异常抛出。这允许用户在使用属性
errorCode
时处理错误。
版本 1.0.8 - 更新日志
- 添加了 API 方法
setOptions([数组])
,允许一次性设置多个选项。 - 修复了问题 #30。
版本 1.0.7 - 更新日志
- 修复了在调用
getInfo($opt)
之前未初始化 cURL 的getInfo([, int $opt = 0 ])
异常。
版本 1.0.6 - 更新日志
- 添加了
getInfo([, int $opt = 0 ])
方法来检索 https://php.ac.cn/manual/de/function.curl-getinfo.php 数据。
版本 1.0.5 - 更新日志
- 使
body
回调不再依赖于 HTTP-状态代码。现在可以在任何 HTTP-状态代码下检索body
数据。 - 修复了问题 #19,其中覆盖默认设置会破坏选项。
- 添加了超时响应处理。
$curl->responseCode = 'timeout'
版本 1.0.4 - 更新日志
CURLOPT_RETURNTRANSFER
现在默认设置为 true - #18- 调整了 Readme.md。
版本 1.0.3 - 更新日志
- 修复了用户选项的覆盖。 #7
- 提供了漂亮的 PHP 示例格式。
- 将
parent::init();
行为移动到 unitTest 控制器。
版本 1.0.2 - 更新日志
- 添加了自定义参数支持
- 添加了自定义状态码支持
- 添加了 POST-参数支持,并在 Readme 中提供了一个示例
- 从请求函数中移除了 "body" 支持。请现在使用 "CURLOPT_POSTFIELDS" 来设置 body。
- 修改了 Readme
版本 1.0.1 - 更新日志
- 移除了小部件支持
- 修改了一些拼写错误 + 在readme.md中添加了更多示例
版本 1.0 - 更新日志
- 官方稳定版本
感谢
马利亚诺·库斯蒂尔 (@mcustiel)
... 以及所有其他贡献者。