Easy working cURL extension for Yii2, including RESTful support:
- POST
- GET
- HEAD
- PUT
- PATCH
- DELETE
- Yii2
- PHP 5.4+
- Curl and php-curl installed
The preferred way to install this extension is through composer.
php composer.phar 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 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);
- Fixed issue with patch request.
- Fully added functionalTests for 100% coverage.
- Fixed linslin#59
- Fixed linslin#57
- Fixed wrong parameter parsing into
_httpRequest()
(thanks to yemexx1) - Added JSON decode functions tests (thanks to yemexx1)
- Added
setHeaders() [array]
helper. - Added
setPostParams() [array]
helper. - Added
setGetParams() [array]
helper. - Added
setRequestBody() [string]
helper. - Added
getUrl()
helper. - Added API attribute
errorText [string|null]
- holds a string describing the given error code - linslin#49. - Added functionTests to ensure stability.
- Allow PHP class goodness - linslin#52.
- Fixed header explode - linslin#51.
- Added API attribute
responseHeaders [array|null]
which returns an array of all response headers. - Changed
_defaultOptions[CURLOPT_HEADER]
totrue
. - Profile debugging is only active if constant
YII_DEBUG
istrue
.
- Fixed PHP notice linslin#39.
- Added API attribute
responseCode [string|null]
which holds the HTTP response code. - Added API attribute
responseCharset [string|null]
which holds the response charset. - Added API attribute
responseLength [integer|null]
which holds the response length. - Added API attribute
errorCode
which holds the a integer code error like described here: https://curl.haxx.se/libcurl/c/libcurl-errors.html. - Fixed Issue https://github.com/linslin/Yii2-Curl/issues//36.
- Fixed Issue https://github.com/linslin/Yii2-Curl/issues//37 and removed exception throwing on curl fail. This allow the user to handle the error while using attribute
errorCode
.
- Added API method
setOptions([array])
which allows to setup multiple options at once. - Fixed Issue linslin#30.
- Fixed
getInfo([, int $opt = 0 ])
exception were cURL wasn't initialized before callinggetInfo($opt)
.
- Added
getInfo([, int $opt = 0 ])
method to retrieve http://php.net/manual/de/function.curl-getinfo.php data.
- Made
body
callback not depending on HTTP-Status codes anymore. You can retrievebody
data on any HTTP-Status now. - Fixed Issue linslin#19 where override default settings break options.
- Added timeout response handling.
$curl->responseCode = 'timeout'
CURLOPT_RETURNTRANSFER
is now set to true on default - linslin#18- Readme.md adjustments.
- Fixed override of user options. linslin#7
- Nice formatted PHP-examples.
- Moved
parent::init();
behavior into unitTest Controller.
- Added custom params support
- Added custom status code support
- Added POST-Param support and a readme example
- Removed "body" support at request functions. Please use "CURLOPT_POSTFIELDS" to setup a body now.
- Readme modifications
- Removed widget support
- Edited some spellings + added more examples into readme.md
- Official stable release