z010107 / yii2-curl-ext
为 Yii2 提供简单易用的 cURL 扩展,支持 RESTful
1.1.0
2016-04-18 10:30 UTC
Requires
- ext-curl: *
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-09-26 00:27:38 UTC
README
为 Yii2 提供的酷炫 cURL 扩展,包括 RESTful 支持
- POST
- GET
- HEAD
- PUT
- DELETE
需求
- Yii2
- PHP 5.4+
- 已安装 Curl 和 php-curl
安装
推荐通过 composer 来安装此扩展。
php composer.phar require --prefer-dist z010107/yii2-curl-ext "*"
使用
扩展安装后,只需在代码中简单使用即可。以下示例展示了如何处理一个简单的 GET 请求。
<?php /** * Yii2 test controller * * @category Web-yii2-example * @package yii2-curl-example * @author Nils Gajsek <info@linslin.org> * @author Krasilnikov Andrey <z010107@gmail.com> * @copyright 2013-2015 Nils Gajsek<info@linslin.org> * @license https://open-source.org.cn/licenses/MIT MIT Public * @version 1.0.7 * @link http://www.linslin.org * */ namespace app\controllers; use yii\web\Controller; use linslin\yii2\curl; class TestController extends Controller { /** * Yii action controller */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], ]; } /** * cURL GET example */ public function actionGetExample() { //Init curl $curl = new curl\Curl(); //get http://example.com/ $response = $curl->get('http://example.com/'); //get http://example.com/ and get response headers $response = $curl->get('http://example.com/'); $headers = $curl->getHeaders(); } /** * cURL POST example with post body params. */ public function actionPostExample() { //Init curl $curl = new curl\Curl(); //post http://example.com/ $response = $curl->setOption( CURLOPT_POSTFIELDS, http_build_query(array( 'myPostField' => 'value' ) )) ->post('http://example.com/'); } /** * cURL multiple POST example one after one */ public function actionMultipleRequest() { //Init curl $curl = new curl\Curl(); //post http://example.com/ $response = $curl->setOption( CURLOPT_POSTFIELDS, http_build_query(array( 'myPostField' => 'value' ) )) ->post('http://example.com/'); //post http://example.com/, reset request before $response = $curl->reset() ->setOption( CURLOPT_POSTFIELDS, http_build_query(array( 'myPostField' => 'value' ) )) ->post('http://example.com/'); } /** * cURL advanced GET example with HTTP status codes */ public function actionGetAdvancedExample() { //Init curl $curl = new curl\Curl(); //get http://example.com/ $response = $curl->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; } } /** * cURL timeout chaining/handling */ public function actionHandleTimeoutExample() { //Init curl $curl = new curl\Curl(); //get http://www.google.com:81/ -> timeout $response = $curl->post('http://www.google.com:81/'); // 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; } } }
变更日志
版本 1.1.0 - 变更日志
- 修复了
getHeaders()
返回的数组格式化问题
版本 1.0.8 - 变更日志
- 添加了
getHeaders()
方法以获取响应头
版本 1.0.7 - 变更日志
- 修复了在调用
getInfo($opt)
之前未初始化 cURL 的异常
版本 1.0.6 - 变更日志
- 添加了
getInfo([, int $opt = 0 ])
方法以获取 https://php.ac.cn/manual/de/function.curl-getinfo.php 数据。
版本 1.0.5 - 变更日志
- 使
body
回调不再依赖于 HTTP-状态代码。现在可以在任何 HTTP-状态下获取body
数据。 - 修复了 Issue linslin#19 中的问题,即覆盖默认设置会破坏选项。
- 添加了超时响应处理。
$curl->responseCode = 'timeout'
版本 1.0.4 - 变更日志
CURLOPT_RETURNTRANSFER
默认设置为 true - linslin#18- 调整了 Readme.md
版本 1.0.3 - 变更日志
- 修复了覆盖用户选项的问题。 linslin#7
- 添加了漂亮的 PHP 示例。
- 将
parent::init();
的行为移动到了单元测试控制器。
版本 1.0.2 - 变更日志
- 添加了自定义参数支持
- 添加了自定义状态码支持
- 添加了 POST 参数支持和 Readme 示例
- 从请求函数中移除了 "body" 支持。请现在使用 "CURLOPT_POSTFIELDS" 来设置 body。
- 修改了 Readme
版本 1.0.1 - 变更日志
- 移除了小部件支持
- 修改了一些拼写错误 + 在 readme.md 中添加了更多示例
版本 1.0 - 变更日志
- 官方稳定版本