qfsx / yii2-curl
为Yii2提供简单且易用的cURL扩展,支持RESTful
1.0.2
2022-10-28 20:43 UTC
Requires
- php: ^7.0|^8.0
- ext-curl: *
- ext-json: *
- yiisoft/yii2: ~2.0.14
This package is not auto-updated.
Last update: 2024-10-01 05:34:25 UTC
README
为Yii2提供简单易用的cURL扩展,包括RESTful支持
- POST
- GET
- HEAD
- PUT
- PATCH
- DELETE
- OPTIONS
需求
- Yii2
- PHP >=7.0
- ext-curl, ext-json, 和 php-curl 已安装
安装
推荐通过composer来安装此扩展。
php composer.phar require --prefer-dist qfsx/yii2-curl "*"
使用
扩展安装后,您可以直接在代码中使用它。以下示例展示了如何处理简单的GET请求。
use qfsx\yii2\curl\Curl; $curl = new 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(); $response = $curl->setGetParams([ 'key' => 'value', 'secondKey' => 'secondValue' ]) ->get('http://example.com/');
// POST URL form-urlencoded $curl = new Curl(); $response = $curl->setPostParams([ 'key' => 'value', 'secondKey' => 'secondValue' ]) ->post('http://example.com/');
// POST RAW JSON $curl = new 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(); $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(); $response = $curl->setRawPostData('<?xml version="1.0" encoding="UTF-8"?><someNode>Test</someNode>') ->post('http://example.com/');
// POST with special headers $curl = new 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(); $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(); $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测试。在Windows上运行vendor\bin\codecept.bat run
。
版本 1.0.2 - 更新日志
- PHP 8.0+
版本 1.0.1 - 更新日志
- 添加OPTIONS请求
版本 1.0 - 更新日志
- 官方稳定版本