2017 © Pedro Peláez
 

yii2-extension yii2-curl

Easy and nice cURL extension with RESTful support for Yii2

image

alkurn/yii2-curl

Easy and nice cURL extension with RESTful support for Yii2

  • Tuesday, June 13, 2017
  • by ganesh.alkurn
  • Repository
  • 1 Watchers
  • 0 Stars
  • 92 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 23 % Grown

The README.md

yii2-curl extension

Latest Stable Version Latest Master Build Test Coverage Total Downloads License, (*1)

Easy working cURL extension for Yii2, including RESTful support:, (*2)

  • 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., (*3)

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., (*4)

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);

The Versions

13/06 2017

dev-master

9999999-dev

Easy and nice cURL extension with RESTful support for Yii2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Ganesh Alkurn

extension yii2 restful curl