Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ before_script:
- composer self-update
- composer install --dev --prefer-source

script:
- ./vendor/bin/phpcs --warning-severity=0 --standard=PSR2 src

php:
- 5.3
- 5.4
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*"
"phpunit/phpunit": "3.7.*",
"squizlabs/php_codesniffer": "~2.1"
},
"autoload": {
"psr-0": {
Expand Down
373 changes: 197 additions & 176 deletions src/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,180 +2,201 @@

namespace Curl;

class Curl {

// The HTTP authentication method(s) to use.

const AUTH_BASIC = CURLAUTH_BASIC;
const AUTH_DIGEST = CURLAUTH_DIGEST;
const AUTH_GSSNEGOTIATE = CURLAUTH_GSSNEGOTIATE;
const AUTH_NTLM = CURLAUTH_NTLM;
const AUTH_ANY = CURLAUTH_ANY;
const AUTH_ANYSAFE = CURLAUTH_ANYSAFE;

const USER_AGENT = 'PHP Curl/1.1 (+https://github.com/mod-php/curl)';

private $_cookies = array();
private $_headers = array();

public $curl;

public $error = FALSE;
public $error_code = 0;
public $error_message = NULL;

public $curl_error = FALSE;
public $curl_error_code = 0;
public $curl_error_message = NULL;

public $http_error = FALSE;
public $http_status_code = 0;
public $http_error_message = NULL;

public $request_headers = NULL;
public $response_headers = NULL;
public $response = NULL;

public function __construct() {

if (!extension_loaded('curl')) {
throw new \ErrorException('cURL library is not loaded');
}

$this->init();
}

public function get($url, $data = array()) {
if (count($data) > 0)
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
else
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_HTTPGET, TRUE);
$this->_exec();
}

public function post($url, $data=array()) {
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_POST, TRUE);
$data = http_build_query($data);
$this->setopt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
}

public function put($url, $data=array()) {
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
$this->_exec();
}

public function patch($url, $data=array()) {
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH');
$this->setopt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
}

public function delete($url, $data=array()) {
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
$this->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE');
$this->_exec();
}

public function setBasicAuthentication($username, $password) {
$this->setHttpAuth(self::AUTH_BASIC);
$this->setopt(CURLOPT_USERPWD, $username . ':' . $password);
}

protected function setHttpAuth($httpauth) {
$this->setOpt(CURLOPT_HTTPAUTH, $httpauth);
}

public function setHeader($key, $value) {
$this->_headers[$key] = $key . ': ' . $value;
$this->setopt(CURLOPT_HTTPHEADER, array_values($this->_headers));
}

public function setUserAgent($user_agent) {
$this->setopt(CURLOPT_USERAGENT, $user_agent);
}

public function setReferrer($referrer) {
$this->setopt(CURLOPT_REFERER, $referrer);
}

public function setCookie($key, $value) {
$this->_cookies[$key] = $value;
$this->setopt(CURLOPT_COOKIE, http_build_query($this->_cookies, '', '; '));
}

public function setOpt($option, $value) {
return curl_setopt($this->curl, $option, $value);
}

public function verbose($on=TRUE) {
$this->setopt(CURLOPT_VERBOSE, $on);
}

public function close() {
if (is_resource($this->curl)) {
curl_close($this->curl);
}
}

public function reset() {
$this->close();
$this->_cookies = array();
$this->_headers = array();
$this->error = FALSE;
$this->error_code = 0;
$this->error_message = NULL;
$this->curl_error = FALSE;
$this->curl_error_code = 0;
$this->curl_error_message = NULL;
$this->http_error = FALSE;
$this->http_status_code = 0;
$this->http_error_message = NULL;
$this->request_headers = NULL;
$this->response_headers = NULL;
$this->response = NULL;
$this->init();
}

public function _exec() {
$this->response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
$this->curl_error_message = curl_error($this->curl);
$this->curl_error = !($this->curl_error_code === 0);
$this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
$this->http_error = in_array(floor($this->http_status_code / 100), array(4, 5));
$this->error = $this->curl_error || $this->http_error;
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;

$this->request_headers = preg_split('/\r\n/', curl_getinfo($this->curl, CURLINFO_HEADER_OUT), NULL, PREG_SPLIT_NO_EMPTY);
$this->response_headers = '';
if (!(strpos($this->response, "\r\n\r\n") === FALSE)) {
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
if ($response_header === 'HTTP/1.1 100 Continue') {
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
}
$this->response_headers = preg_split('/\r\n/', $response_header, NULL, PREG_SPLIT_NO_EMPTY);
}

$this->http_error_message = $this->error ? (isset($this->response_headers['0']) ? $this->response_headers['0'] : '') : '';
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;

return $this->error_code;
}

public function __destruct() {
$this->close();
}

private function init() {
$this->curl = curl_init();
$this->setUserAgent(self::USER_AGENT);
$this->setopt(CURLINFO_HEADER_OUT, TRUE);
$this->setopt(CURLOPT_HEADER, TRUE);
$this->setopt(CURLOPT_RETURNTRANSFER, TRUE);
}
class Curl
{

// The HTTP authentication method(s) to use.

const AUTH_BASIC = CURLAUTH_BASIC;
const AUTH_DIGEST = CURLAUTH_DIGEST;
const AUTH_GSSNEGOTIATE = CURLAUTH_GSSNEGOTIATE;
const AUTH_NTLM = CURLAUTH_NTLM;
const AUTH_ANY = CURLAUTH_ANY;
const AUTH_ANYSAFE = CURLAUTH_ANYSAFE;

const USER_AGENT = 'PHP Curl/1.1 (+https://github.com/mod-php/curl)';

private $_cookies = array();
private $_headers = array();

public $curl;

public $error = false;
public $error_code = 0;
public $error_message = null;

public $curl_error = false;
public $curl_error_code = 0;
public $curl_error_message = null;

public $http_error = false;
public $http_status_code = 0;
public $http_error_message = null;

public $request_headers = null;
public $response_headers = null;
public $response = null;

public function __construct()
{

if (!extension_loaded('curl')) {
throw new \ErrorException('cURL library is not loaded');
}

$this->init();
}

public function get($url, $data = array())
{
if (count($data) > 0) {
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
} else {
$this->setopt(CURLOPT_URL, $url);
}
$this->setopt(CURLOPT_HTTPGET, true);
$this->_exec();
}

public function post($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_POST, true);
$data = http_build_query($data);
$this->setopt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
}

public function put($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
$this->_exec();
}

public function patch($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url);
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH');
$this->setopt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
}

public function delete($url, $data = array())
{
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
$this->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE');
$this->_exec();
}

public function setBasicAuthentication($username, $password)
{
$this->setHttpAuth(self::AUTH_BASIC);
$this->setopt(CURLOPT_USERPWD, $username . ':' . $password);
}

protected function setHttpAuth($httpauth)
{
$this->setOpt(CURLOPT_HTTPAUTH, $httpauth);
}

public function setHeader($key, $value)
{
$this->_headers[$key] = $key . ': ' . $value;
$this->setopt(CURLOPT_HTTPHEADER, array_values($this->_headers));
}

public function setUserAgent($user_agent)
{
$this->setopt(CURLOPT_USERAGENT, $user_agent);
}

public function setReferrer($referrer)
{
$this->setopt(CURLOPT_REFERER, $referrer);
}

public function setCookie($key, $value)
{
$this->_cookies[$key] = $value;
$this->setopt(CURLOPT_COOKIE, http_build_query($this->_cookies, '', '; '));
}

public function setOpt($option, $value)
{
return curl_setopt($this->curl, $option, $value);
}

public function verbose($on = true)
{
$this->setopt(CURLOPT_VERBOSE, $on);
}

public function close()
{
if (is_resource($this->curl)) {
curl_close($this->curl);
}
}

public function reset()
{
$this->close();
$this->_cookies = array();
$this->_headers = array();
$this->error = false;
$this->error_code = 0;
$this->error_message = null;
$this->curl_error = false;
$this->curl_error_code = 0;
$this->curl_error_message = null;
$this->http_error = false;
$this->http_status_code = 0;
$this->http_error_message = null;
$this->request_headers = null;
$this->response_headers = null;
$this->response = null;
$this->init();
}

public function _exec()
{
$this->response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
$this->curl_error_message = curl_error($this->curl);
$this->curl_error = !($this->curl_error_code === 0);
$this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
$this->http_error = in_array(floor($this->http_status_code / 100), array(4, 5));
$this->error = $this->curl_error || $this->http_error;
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;

$this->request_headers = preg_split('/\r\n/', curl_getinfo($this->curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
$this->response_headers = '';
if (!(strpos($this->response, "\r\n\r\n") === false)) {
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
if ($response_header === 'HTTP/1.1 100 Continue') {
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
}
$this->response_headers = preg_split('/\r\n/', $response_header, null, PREG_SPLIT_NO_EMPTY);
}

$this->http_error_message = $this->error ? (isset($this->response_headers['0']) ? $this->response_headers['0'] : '') : '';
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;

return $this->error_code;
}

public function __destruct()
{
$this->close();
}

private function init()
{
$this->curl = curl_init();
$this->setUserAgent(self::USER_AGENT);
$this->setopt(CURLINFO_HEADER_OUT, true);
$this->setopt(CURLOPT_HEADER, true);
$this->setopt(CURLOPT_RETURNTRANSFER, true);
}
}