2017 © Pedro Peláez
 

yii2-extension yii2-curl

A base curl wrapper for Yii2 Framework

image

maxwen/yii2-curl

A base curl wrapper for Yii2 Framework

  • Friday, December 23, 2016
  • by max.wen
  • Repository
  • 1 Watchers
  • 8 Stars
  • 725 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 174 Forks
  • 0 Open issues
  • 2 Versions
  • 4 % Grown

The README.md

Yii-curl

Forked from shuber/curl, A base curl wrapper for Yii2 Framework, (*1)

Installation via Composer

composer require maxwen/yii2-curl, (*2)

Usage

Performing a Request

The Curl object supports 5 types of requests: HEAD, GET, POST, PUT, and DELETE. You must specify a url to request and optionally specify an associative array or string of variables to send along with it., (*3)

// set options
$curl = new \maxwen\yii\curl\Curl();
$curlOptions = [
        // 'CURLOPT_SSL_VERIFYHOST' => true,
        'CURLOPT_SSL_VERIFYPEER'    => false,
        'CURLOPT_RETURNTRANSFER'    => true,
        'CURLOPT_TIMEOUT'           => 30,
    ];
$curl->options = $curlOptions;
// send requests
$response = $curl->head($url, $vars = array());
$response = $curl->get($url, $vars = array()); # The Curl object will append the array of $vars to the $url as a query string
$response = $curl->post($url, $vars = array());
$response = $curl->put($url, $vars = array());
$response = $curl->delete($url, $vars = array());

To use a custom request methods, you can call the request method:, (*4)

$response = $curl->request('YOUR_CUSTOM_REQUEST_TYPE', $url, $vars = array());

All of the built in request methods like put and get simply wrap the request method. For example, the post method is implemented like:, (*5)

function post($url, $vars = array()) {
    return $this->request('POST', $url, $vars);
}

Examples:, (*6)

$response = $curl->get('google.com?q=test');

# The Curl object will append '&some_variable=some_value' to the url
$response = $curl->get('google.com?q=test', array('some_variable' => 'some_value'));

$response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));

All requests return a CurlResponse object (see below) or false if an error occurred. You can access the error string with the $curl->error() method., (*7)

The CurlResponse Object

A normal CURL request will return the headers and the body in one response string. This class parses the two and places them into separate properties., (*8)

For example, (*9)

$response = $curl->get('google.com');
echo $response->body; # A string containing everything in the response except for the headers
print_r($response->headers); # An associative array containing the response headers

Which would display something like, (*10)

<html>
<head>
<title>Google.com</title>
</head>
<body>
Some more html...
</body>
</html>

Array
(
    [Http-Version] => 1.0
    [Status-Code] => 200
    [Status] => 200 OK
    [Cache-Control] => private
    [Content-Type] => text/html; charset=ISO-8859-1
    [Date] => Wed, 07 May 2008 21:43:48 GMT
    [Server] => gws
    [Connection] => close
)

The CurlResponse class defines the magic __toString() method which will return the response body, so echo $response is the same as echo $response->body, (*11)

By default, cookies will be stored in a file called curl_cookie.txt. You can change this file's name by setting it like this, (*12)

$curl->cookie_file = 'some_other_filename';

This allows you to maintain a session across requests, (*13)

Basic Configuration Options

You can easily set the referer or user-agent, (*14)

$curl->referer = 'http://google.com';
$curl->user_agent = 'some user agent string';

You may even set these headers manually if you wish (see below), (*15)

Setting Custom Headers

You can set custom headers to send with the request, (*16)

$curl->headers['Host'] = 12.345.678.90;
$curl->headers['Some-Custom-Header'] = 'Some Custom Value';

Setting Custom CURL request options

By default, the Curl object will follow redirects. You can disable this by setting:, (*17)

$curl->follow_redirects = false;

You can set/override many different options for CURL requests (see the curl_setopt documentation for a list of them), (*18)

# any of these will work
$curl->options['AUTOREFERER'] = true;
$curl->options['autoreferer'] = true;
$curl->options['CURLOPT_AUTOREFERER'] = true;
$curl->options['curlopt_autoreferer'] = true;

The Versions

23/12 2016

dev-master

9999999-dev

A base curl wrapper for Yii2 Framework

  Sources   Download

MIT

The Requires

 

by Max Wen

curl extension yii2 restful

08/01 2016

1.0

1.0.0.0

A base curl wrapper for Yii2 Framework

  Sources   Download

MIT

The Requires

 

by Max Wen

curl extension yii2 restful