skeeks / yii2-curl
易于使用且功能齐全的cURL扩展,支持Yii2的RESTful API
1.1.0
2017-05-08 17:51 UTC
Requires
- yiisoft/yii2: *
This package is auto-updated.
Last update: 2024-08-29 04:13:31 UTC
README
为Yii2提供的酷炫curl扩展,包括RESTful支持
- POST
- GET
- HEAD
- PUT
- DELETE
- PATCH
- OPTIONS
需求
- Yii2
- PHP 5.4+
- 安装Curl和php-curl
安装
推荐通过Composer来安装此扩展。
php composer.phar require --prefer-dist skeeks/yii2-curl "*"
使用方法
扩展安装完成后,只需在代码中简单使用即可。以下示例展示了如何处理一个简单的GET请求。
<?php /** * Yii2 test controller * * @category Web-yii2-example * @package yii2-curl-example * @license https://open-source.org.cn/licenses/MIT MIT Public * */ namespace app\controllers; use yii\web\Controller; use skeeks\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 200: //success logic here break; case 404: //404 Error logic here break; } } }