|
|
il y a 6 ans | |
|---|---|---|
| .. | ||
| Curl.php | il y a 6 ans | |
| README.md | il y a 6 ans | |
| composer.json | il y a 6 ans | |
Cool working curl extension for Yii2, including RESTful support:
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.
<?php
/**
* Yii2 test controller
*
* @category Web-yii2-example
* @package yii2-curl-example
* @author Nils Gajsek <info@linslin.org>
* @copyright 2013-2017 Nils Gajsek <info@linslin.org>
* @license http://opensource.org/licenses/MIT MIT Public
* @version 1.0.11
* @link http://www.linslin.org
*
*/
namespace app\controllers;
use yii\web\Controller;
use linslin\yii2\curl;
class TestController extends Controller
{
/**
* Yii action controller
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
/**
* cURL GET example and accessing response headers
*/
public function actionGetExample()
{
//Init curl
$curl = new curl\Curl();
//get http://example.com/
$response = $curl->get('http://example.com/');
$responseHeaders = $curl->responseHeaders;
}
/**
* 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 'timeout':
//timeout error logic here
break;
case 200:
//success logic here
break;
case 404:
//404 Error logic here
break;
}
}
/**
* cURL timeout chaining/handling
*/
public function actionHandleTimeoutExample()
{
//Init curl
$curl = new curl\Curl();
//get http://www.google.com:81/ -> timeout
$response = $curl->post('http://www.google.com:81/');
// 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;
}
}
/**
* cURL error handling
*/
public function actionHandleHostUknownExample()
{
//Init curl
$curl = new curl\Curl();
//get http://www.google.com:81/ -> timeout
$response = $curl->post('http://www.xyz-no-one-set.nope');
// List of curl error codes here https://curl.haxx.se/libcurl/c/libcurl-errors.html
switch ($curl->errorCode) {
case 6:
//host unkown example
break;
}
}
}
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.