Skip to content

Commit 97b7637

Browse files
committed
PSR2 compliance
1 parent e53f592 commit 97b7637

File tree

3 files changed

+202
-177
lines changed

3 files changed

+202
-177
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ before_script:
44
- composer self-update
55
- composer install --dev --prefer-source
66

7+
script:
8+
- ./vendor/bin/phpcs --warning-severity=0 --standard=PSR2 src
9+
710
php:
811
- 5.3
912
- 5.4

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"ext-curl": "*"
2626
},
2727
"require-dev": {
28-
"phpunit/phpunit": "3.7.*"
28+
"phpunit/phpunit": "3.7.*",
29+
"squizlabs/php_codesniffer": "~2.1"
2930
},
3031
"autoload": {
3132
"psr-0": {

src/Curl/Curl.php

Lines changed: 197 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -2,180 +2,201 @@
22

33
namespace Curl;
44

5-
class Curl {
6-
7-
// The HTTP authentication method(s) to use.
8-
9-
const AUTH_BASIC = CURLAUTH_BASIC;
10-
const AUTH_DIGEST = CURLAUTH_DIGEST;
11-
const AUTH_GSSNEGOTIATE = CURLAUTH_GSSNEGOTIATE;
12-
const AUTH_NTLM = CURLAUTH_NTLM;
13-
const AUTH_ANY = CURLAUTH_ANY;
14-
const AUTH_ANYSAFE = CURLAUTH_ANYSAFE;
15-
16-
const USER_AGENT = 'PHP Curl/1.1 (+https://github.com/mod-php/curl)';
17-
18-
private $_cookies = array();
19-
private $_headers = array();
20-
21-
public $curl;
22-
23-
public $error = FALSE;
24-
public $error_code = 0;
25-
public $error_message = NULL;
26-
27-
public $curl_error = FALSE;
28-
public $curl_error_code = 0;
29-
public $curl_error_message = NULL;
30-
31-
public $http_error = FALSE;
32-
public $http_status_code = 0;
33-
public $http_error_message = NULL;
34-
35-
public $request_headers = NULL;
36-
public $response_headers = NULL;
37-
public $response = NULL;
38-
39-
public function __construct() {
40-
41-
if (!extension_loaded('curl')) {
42-
throw new \ErrorException('cURL library is not loaded');
43-
}
44-
45-
$this->init();
46-
}
47-
48-
public function get($url, $data = array()) {
49-
if (count($data) > 0)
50-
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
51-
else
52-
$this->setopt(CURLOPT_URL, $url);
53-
$this->setopt(CURLOPT_HTTPGET, TRUE);
54-
$this->_exec();
55-
}
56-
57-
public function post($url, $data=array()) {
58-
$this->setopt(CURLOPT_URL, $url);
59-
$this->setopt(CURLOPT_POST, TRUE);
60-
$data = http_build_query($data);
61-
$this->setopt(CURLOPT_POSTFIELDS, $data);
62-
$this->_exec();
63-
}
64-
65-
public function put($url, $data=array()) {
66-
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
67-
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
68-
$this->_exec();
69-
}
70-
71-
public function patch($url, $data=array()) {
72-
$this->setopt(CURLOPT_URL, $url);
73-
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH');
74-
$this->setopt(CURLOPT_POSTFIELDS, $data);
75-
$this->_exec();
76-
}
77-
78-
public function delete($url, $data=array()) {
79-
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
80-
$this->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE');
81-
$this->_exec();
82-
}
83-
84-
public function setBasicAuthentication($username, $password) {
85-
$this->setHttpAuth(self::AUTH_BASIC);
86-
$this->setopt(CURLOPT_USERPWD, $username . ':' . $password);
87-
}
88-
89-
protected function setHttpAuth($httpauth) {
90-
$this->setOpt(CURLOPT_HTTPAUTH, $httpauth);
91-
}
92-
93-
public function setHeader($key, $value) {
94-
$this->_headers[$key] = $key . ': ' . $value;
95-
$this->setopt(CURLOPT_HTTPHEADER, array_values($this->_headers));
96-
}
97-
98-
public function setUserAgent($user_agent) {
99-
$this->setopt(CURLOPT_USERAGENT, $user_agent);
100-
}
101-
102-
public function setReferrer($referrer) {
103-
$this->setopt(CURLOPT_REFERER, $referrer);
104-
}
105-
106-
public function setCookie($key, $value) {
107-
$this->_cookies[$key] = $value;
108-
$this->setopt(CURLOPT_COOKIE, http_build_query($this->_cookies, '', '; '));
109-
}
110-
111-
public function setOpt($option, $value) {
112-
return curl_setopt($this->curl, $option, $value);
113-
}
114-
115-
public function verbose($on=TRUE) {
116-
$this->setopt(CURLOPT_VERBOSE, $on);
117-
}
118-
119-
public function close() {
120-
if (is_resource($this->curl)) {
121-
curl_close($this->curl);
122-
}
123-
}
124-
125-
public function reset() {
126-
$this->close();
127-
$this->_cookies = array();
128-
$this->_headers = array();
129-
$this->error = FALSE;
130-
$this->error_code = 0;
131-
$this->error_message = NULL;
132-
$this->curl_error = FALSE;
133-
$this->curl_error_code = 0;
134-
$this->curl_error_message = NULL;
135-
$this->http_error = FALSE;
136-
$this->http_status_code = 0;
137-
$this->http_error_message = NULL;
138-
$this->request_headers = NULL;
139-
$this->response_headers = NULL;
140-
$this->response = NULL;
141-
$this->init();
142-
}
143-
144-
public function _exec() {
145-
$this->response = curl_exec($this->curl);
146-
$this->curl_error_code = curl_errno($this->curl);
147-
$this->curl_error_message = curl_error($this->curl);
148-
$this->curl_error = !($this->curl_error_code === 0);
149-
$this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
150-
$this->http_error = in_array(floor($this->http_status_code / 100), array(4, 5));
151-
$this->error = $this->curl_error || $this->http_error;
152-
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;
153-
154-
$this->request_headers = preg_split('/\r\n/', curl_getinfo($this->curl, CURLINFO_HEADER_OUT), NULL, PREG_SPLIT_NO_EMPTY);
155-
$this->response_headers = '';
156-
if (!(strpos($this->response, "\r\n\r\n") === FALSE)) {
157-
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
158-
if ($response_header === 'HTTP/1.1 100 Continue') {
159-
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
160-
}
161-
$this->response_headers = preg_split('/\r\n/', $response_header, NULL, PREG_SPLIT_NO_EMPTY);
162-
}
163-
164-
$this->http_error_message = $this->error ? (isset($this->response_headers['0']) ? $this->response_headers['0'] : '') : '';
165-
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;
166-
167-
return $this->error_code;
168-
}
169-
170-
public function __destruct() {
171-
$this->close();
172-
}
173-
174-
private function init() {
175-
$this->curl = curl_init();
176-
$this->setUserAgent(self::USER_AGENT);
177-
$this->setopt(CURLINFO_HEADER_OUT, TRUE);
178-
$this->setopt(CURLOPT_HEADER, TRUE);
179-
$this->setopt(CURLOPT_RETURNTRANSFER, TRUE);
180-
}
5+
class Curl
6+
{
7+
8+
// The HTTP authentication method(s) to use.
9+
10+
const AUTH_BASIC = CURLAUTH_BASIC;
11+
const AUTH_DIGEST = CURLAUTH_DIGEST;
12+
const AUTH_GSSNEGOTIATE = CURLAUTH_GSSNEGOTIATE;
13+
const AUTH_NTLM = CURLAUTH_NTLM;
14+
const AUTH_ANY = CURLAUTH_ANY;
15+
const AUTH_ANYSAFE = CURLAUTH_ANYSAFE;
16+
17+
const USER_AGENT = 'PHP Curl/1.1 (+https://github.com/mod-php/curl)';
18+
19+
private $_cookies = array();
20+
private $_headers = array();
21+
22+
public $curl;
23+
24+
public $error = false;
25+
public $error_code = 0;
26+
public $error_message = null;
27+
28+
public $curl_error = false;
29+
public $curl_error_code = 0;
30+
public $curl_error_message = null;
31+
32+
public $http_error = false;
33+
public $http_status_code = 0;
34+
public $http_error_message = null;
35+
36+
public $request_headers = null;
37+
public $response_headers = null;
38+
public $response = null;
39+
40+
public function __construct()
41+
{
42+
43+
if (!extension_loaded('curl')) {
44+
throw new \ErrorException('cURL library is not loaded');
45+
}
46+
47+
$this->init();
48+
}
49+
50+
public function get($url, $data = array())
51+
{
52+
if (count($data) > 0) {
53+
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
54+
} else {
55+
$this->setopt(CURLOPT_URL, $url);
56+
}
57+
$this->setopt(CURLOPT_HTTPGET, true);
58+
$this->_exec();
59+
}
60+
61+
public function post($url, $data = array())
62+
{
63+
$this->setopt(CURLOPT_URL, $url);
64+
$this->setopt(CURLOPT_POST, true);
65+
$data = http_build_query($data);
66+
$this->setopt(CURLOPT_POSTFIELDS, $data);
67+
$this->_exec();
68+
}
69+
70+
public function put($url, $data = array())
71+
{
72+
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
73+
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PUT');
74+
$this->_exec();
75+
}
76+
77+
public function patch($url, $data = array())
78+
{
79+
$this->setopt(CURLOPT_URL, $url);
80+
$this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH');
81+
$this->setopt(CURLOPT_POSTFIELDS, $data);
82+
$this->_exec();
83+
}
84+
85+
public function delete($url, $data = array())
86+
{
87+
$this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data));
88+
$this->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE');
89+
$this->_exec();
90+
}
91+
92+
public function setBasicAuthentication($username, $password)
93+
{
94+
$this->setHttpAuth(self::AUTH_BASIC);
95+
$this->setopt(CURLOPT_USERPWD, $username . ':' . $password);
96+
}
97+
98+
protected function setHttpAuth($httpauth)
99+
{
100+
$this->setOpt(CURLOPT_HTTPAUTH, $httpauth);
101+
}
102+
103+
public function setHeader($key, $value)
104+
{
105+
$this->_headers[$key] = $key . ': ' . $value;
106+
$this->setopt(CURLOPT_HTTPHEADER, array_values($this->_headers));
107+
}
108+
109+
public function setUserAgent($user_agent)
110+
{
111+
$this->setopt(CURLOPT_USERAGENT, $user_agent);
112+
}
113+
114+
public function setReferrer($referrer)
115+
{
116+
$this->setopt(CURLOPT_REFERER, $referrer);
117+
}
118+
119+
public function setCookie($key, $value)
120+
{
121+
$this->_cookies[$key] = $value;
122+
$this->setopt(CURLOPT_COOKIE, http_build_query($this->_cookies, '', '; '));
123+
}
124+
125+
public function setOpt($option, $value)
126+
{
127+
return curl_setopt($this->curl, $option, $value);
128+
}
129+
130+
public function verbose($on = true)
131+
{
132+
$this->setopt(CURLOPT_VERBOSE, $on);
133+
}
134+
135+
public function close()
136+
{
137+
if (is_resource($this->curl)) {
138+
curl_close($this->curl);
139+
}
140+
}
141+
142+
public function reset()
143+
{
144+
$this->close();
145+
$this->_cookies = array();
146+
$this->_headers = array();
147+
$this->error = false;
148+
$this->error_code = 0;
149+
$this->error_message = null;
150+
$this->curl_error = false;
151+
$this->curl_error_code = 0;
152+
$this->curl_error_message = null;
153+
$this->http_error = false;
154+
$this->http_status_code = 0;
155+
$this->http_error_message = null;
156+
$this->request_headers = null;
157+
$this->response_headers = null;
158+
$this->response = null;
159+
$this->init();
160+
}
161+
162+
public function _exec()
163+
{
164+
$this->response = curl_exec($this->curl);
165+
$this->curl_error_code = curl_errno($this->curl);
166+
$this->curl_error_message = curl_error($this->curl);
167+
$this->curl_error = !($this->curl_error_code === 0);
168+
$this->http_status_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE);
169+
$this->http_error = in_array(floor($this->http_status_code / 100), array(4, 5));
170+
$this->error = $this->curl_error || $this->http_error;
171+
$this->error_code = $this->error ? ($this->curl_error ? $this->curl_error_code : $this->http_status_code) : 0;
172+
173+
$this->request_headers = preg_split('/\r\n/', curl_getinfo($this->curl, CURLINFO_HEADER_OUT), null, PREG_SPLIT_NO_EMPTY);
174+
$this->response_headers = '';
175+
if (!(strpos($this->response, "\r\n\r\n") === false)) {
176+
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
177+
if ($response_header === 'HTTP/1.1 100 Continue') {
178+
list($response_header, $this->response) = explode("\r\n\r\n", $this->response, 2);
179+
}
180+
$this->response_headers = preg_split('/\r\n/', $response_header, null, PREG_SPLIT_NO_EMPTY);
181+
}
182+
183+
$this->http_error_message = $this->error ? (isset($this->response_headers['0']) ? $this->response_headers['0'] : '') : '';
184+
$this->error_message = $this->curl_error ? $this->curl_error_message : $this->http_error_message;
185+
186+
return $this->error_code;
187+
}
188+
189+
public function __destruct()
190+
{
191+
$this->close();
192+
}
193+
194+
private function init()
195+
{
196+
$this->curl = curl_init();
197+
$this->setUserAgent(self::USER_AGENT);
198+
$this->setopt(CURLINFO_HEADER_OUT, true);
199+
$this->setopt(CURLOPT_HEADER, true);
200+
$this->setopt(CURLOPT_RETURNTRANSFER, true);
201+
}
181202
}

0 commit comments

Comments
 (0)