alkurn / yii2-curl
Easy and nice cURL extension with RESTful support for Yii2
Installs: 629
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Type:yii2-extension
Requires
- ext-curl: *
- yiisoft/yii2: *
Requires (Dev)
- codeception/base: ^2.2.3
- codeception/specify: ~0.4.3
- codeception/verify: ~0.3.1
- codeclimate/php-test-reporter: dev-master
- guzzlehttp/guzzle: >=4.1.4 <7.0
- mcustiel/phiremock-codeception-extension: 1.2.4
This package is not auto-updated.
Last update: 2025-03-07 21:54:26 UTC
README
Easy working cURL extension for Yii2, including RESTful support:
- POST
- GET
- HEAD
- PUT
- PATCH
- DELETE
Requirements
- Yii2
- PHP 5.4+
- Curl and php-curl installed
Installation
The preferred way to install this extension is through composer.
php composer.phar require --prefer-dist alkurn/yii2-curl "dev-master"
composer require --prefer-dist alkurn/yii2-curl "dev-master"
Usage
Once the extension is installed, simply use it in your code. The following example shows you how to handling a simple GET Request.
use alkurn\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);