hoksilato / yii2-curl
为Yii2提供简单且优秀的cURL扩展,支持RESTful
1.0.7
2015-12-21 12:24 UTC
Requires
- ext-curl: *
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-10-02 08:50:23 UTC
README
为Yii2提供酷炫的工作cURL扩展,包括RESTful支持
- POST
- GET
- HEAD
- PUT
- DELETE
需求
- Yii2
- PHP 5.4+
- 已安装Curl和php-curl
安装
安装此扩展的首选方式是通过composer。
php composer.phar require --prefer-dist linslin/yii2-curl "*"
使用
扩展安装完成后,只需在代码中简单使用即可。以下示例展示了如何处理一个简单的GET请求。
<?php /** * Yii2 test controller * * @category Web-yii2-example * @package yii2-curl-example * @author Nils Gajsek <info@linslin.org> * @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/'); } /** * 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.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数据。 - 修复了问题linslin#19,其中覆盖默认设置会破坏选项。
- 添加了超时响应处理。
$curl->responseCode = 'timeout'
版本 1.0.4 - 变更日志
CURLOPT_RETURNTRANSFER默认现在设置为true - linslin#18- 调整了readme.md。
版本 1.0.3 - 变更日志
- 修复了覆盖用户选项的问题。linslin#7
- 添加了漂亮的PHP示例格式。
- 将
parent::init();的行为移动到unitTest控制器中。
版本 1.0.2 - 变更日志
- 添加了自定义参数支持
- 添加了自定义状态码支持
- 添加了POST参数支持和一个readme示例
- 在请求函数中移除了"body"支持。现在请使用"CURLOPT_POSTFIELDS"来设置body。
- 修改了readme。
版本 1.0.1 - 变更日志
- 移除了小部件支持
- 修改了一些拼写错误 + 在readme.md中添加了更多示例
版本 1.0 - 变更日志
- 官方稳定版本