|
|
hace 10 meses | |
|---|---|---|
| .. | ||
| tests | hace 10 meses | |
| .gitignore | hace 10 meses | |
| .travis.yml | hace 10 meses | |
| Curl.php | hace 10 meses | |
| README.md | hace 10 meses | |
| codeception.yml | hace 10 meses | |
| composer.json | hace 10 meses | |
| composer.lock | hace 10 meses | |
Easy working cURL extension for Yii2, including RESTful support:
The preferred way to install this extension is through composer.
composer require --prefer-dist linslin/yii2-curl "*"
Once the extension is installed, simply use it in your code. The following example shows you how to handling a simple GET Request.
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 in repository root dir.
Run codeception with clover report XDEBUG_MODE=coverage vendor/bin/codecept run --coverage-xml ./../../build/logs/clover.xml.
On windows run vendor\bin\codecept.bat run.
On windows with clover report run vendor\bin\codecept.bat run --coverage-xml ./../../build/logs/clover.xml.setRawPostData([mixed]) [this] which allows you to post any data format.unsetHeader([string header]) [this] helper which allows you to unset one specific header.setHeader([string header, string value]) [this] helper which allows you to set one specific header.getRequestHeaders() [array] helper which returns all request headers as an array.getRequestHeader([string headerKey]) [string|null] helper which returns a specific request header as an string.getRequestHeaders() and getRequestHeader()._httpRequest() (thanks to yemexx1)setHeaders() [array] helper.setPostParams() [array] helper.setGetParams() [array] helper.setRequestBody() [string] helper.getUrl() helper.errorText [string|null] - holds a string describing the given error code - https://github.com/linslin/Yii2-Curl/issues/49.responseHeaders [array|null] which returns an array of all response headers._defaultOptions[CURLOPT_HEADER] to true.YII_DEBUG is true.responseCode [string|null] which holds the HTTP response code.responseCharset [string|null] which holds the response charset.responseLength [integer|null] which holds the response length.errorCode which holds the a integer code error like described here: https://curl.haxx.se/libcurl/c/libcurl-errors.html.errorCode.setOptions([array]) which allows to setup multiple options at once.getInfo([, int $opt = 0 ]) exception were cURL wasn't initialized before calling getInfo($opt).getInfo([, int $opt = 0 ]) method to retrieve http://php.net/manual/de/function.curl-getinfo.php data.body callback not depending on HTTP-Status codes anymore. You can retrieve body data on any HTTP-Status now.$curl->responseCode = 'timeout'CURLOPT_RETURNTRANSFER is now set to true on default - https://github.com/linslin/Yii2-Curl/issues/18parent::init(); behavior into unitTest Controller.Mariano Custiel (@mcustiel)
... and all other contributors.