Skip to content

Commit 29b099c

Browse files
authored
v2 (#2)
1 parent 5d9d9d8 commit 29b099c

16 files changed

+621
-276
lines changed

.travis.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
language: php
22
php:
3-
- '5.4'
43
- '5.5'
54
- '5.6'
65
- '7.0'
6+
- '7.1'
77
- hhvm
88
- nightly
99

1010
before_script:
1111
- composer install
12+
13+
script:
14+
- vendor/bin/phpcs
15+
- vendor/bin/phpunit

README.md

+1-9
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,14 @@ Via [composer](https://getcomposer.org):
1212
`$ composer require "phpcurl/curlhttp"`
1313

1414
##Usage
15-
16-
It is really that easy.
17-
1815
```php
1916
<?php
20-
require_once 'vendor/autoload.php';
2117
use PHPCurl\CurlHttp\HttpClient;
2218

2319
$http = new HttpClient();
2420

25-
$http->setOptions([
26-
CURLOPT_FOLLOWLOCATION => false, // Any arbitrary curl options you want
27-
]);
28-
2921
$response = $http->post('http://example.com/?a=b', 'my post data', ['User-Agent: My php crawler']);
30-
// Supported: get(), post(), head(), post(), put(), delete(), custom methods
22+
// Supported: get(), post(), head(), post(), put(), delete()
3123

3224
$body = $response->getBody(); // Response body, string
3325

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
}
2525
],
2626
"require": {
27-
"php": ">=5.3.0",
28-
"phpcurl/curlwrapper": "^1"
27+
"php": ">=5.5",
28+
"phpcurl/curlwrapper": "^2.1"
2929
},
3030
"require-dev": {
31-
"phpunit/phpunit": "4.*",
32-
"weew/php-http-server": "^1.0.0"
31+
"phpunit/phpunit": "^4.0 || ^5.0",
32+
"squizlabs/php_codesniffer": "^2.0",
33+
"symfony/process": "^2"
3334
},
3435
"autoload": {
3536
"psr-4": {

phpcs.xml.dist

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<ruleset>
3+
<file>src</file>
4+
<file>test</file>
5+
<exclude-pattern>vendor/*</exclude-pattern>
6+
<rule ref="PSR2"/>
7+
</ruleset>

phpunit.xml.dist

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<phpunit
33
bootstrap="vendor/autoload.php"
4-
backupGlobals="true"
5-
backupStaticAttributes="true"
64
colors="true"
75
convertErrorsToExceptions="true"
86
convertNoticesToExceptions="true"

src/Executor.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
namespace PHPCurl\CurlHttp;
3+
4+
use PHPCurl\CurlWrapper\Curl;
5+
use PHPCurl\CurlWrapper\CurlInterface;
6+
7+
class Executor implements ExecutorInterface
8+
{
9+
/**
10+
* @var CurlInterface
11+
*/
12+
private $curl;
13+
14+
/**
15+
* @param CurlInterface $curl
16+
*/
17+
public function __construct(CurlInterface $curl = null)
18+
{
19+
$this->curl = $curl ?: new Curl();
20+
}
21+
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function execute($url, array $options)
26+
{
27+
$options[CURLOPT_RETURNTRANSFER] = true;
28+
$options[CURLOPT_HEADER] = true;
29+
30+
$this->curl->init($url);
31+
$this->curl->setOptArray($options);
32+
$response = $this->curl->exec();
33+
if (false === $response) {
34+
throw NoResponse::fromCurl($this->curl);
35+
}
36+
$info = $this->curl->getInfo();
37+
return HttpResponse::fromCurl($response, $info);
38+
}
39+
}

src/ExecutorInterface.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
namespace PHPCurl\CurlHttp;
3+
4+
interface ExecutorInterface
5+
{
6+
/**
7+
* Init curl with $url, set $options, execute, return response
8+
* @param string $url Goes to curl_init()
9+
* @param array $options Goes to curl_setopt()
10+
* @return HttpResponse
11+
* @throws NoResponse If no response was received
12+
*/
13+
public function execute($url, array $options);
14+
}

src/HttpClient.php

+62-107
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,110 @@
11
<?php
22
namespace PHPCurl\CurlHttp;
33

4-
use \PHPCurl\CurlWrapper\Curl;
5-
64
/**
75
* Minimalistic HTTP client
86
*/
9-
class HttpClient
7+
class HttpClient implements HttpClientInterface
108
{
11-
const USE_EXCEPTIONS = true;
12-
139
/**
14-
* Numer of attempts to make per each call
15-
* @var int
10+
* @var ExecutorInterface
1611
*/
17-
protected $attempts = 1;
12+
private $executor;
1813

1914
/**
20-
* @var array
15+
* HttpClient constructor.
16+
* @param ExecutorInterface $executor
2117
*/
22-
private $userOptions = array();
18+
public function __construct(ExecutorInterface $executor = null)
19+
{
20+
$this->executor = $executor ?: new Executor();
21+
}
2322

2423
/**
2524
* HTTP GET
26-
* @param string $url Goes to curl_init()
27-
* @param array $headers Same as CURLOPT_HEADER
25+
* @param string $url Goes to curl_init()
26+
* @param array $headers Same as CURLOPT_HEADER
2827
* @return HttpResponse
2928
*/
30-
public function get($url, array $headers = null)
29+
public function get($url, array $headers = [])
3130
{
32-
$opt = array();
33-
if ($headers) {
34-
$opt[CURLOPT_HTTPHEADER] = $headers;
35-
}
36-
return $this->exec($url, $opt);
31+
return $this->executor->execute(
32+
$url,
33+
[
34+
CURLOPT_HTTPHEADER => $headers,
35+
]
36+
);
3737
}
3838

3939
/**
4040
* HTTP HEAD (implemented using CURLOPT_NOBODY)
41-
* @param string $url Goes to curl_init()
42-
* @param array $headers Same as CURLOPT_HEADER
41+
* @param string $url Goes to curl_init()
42+
* @param array $headers Same as CURLOPT_HEADER
4343
* @return HttpResponse
4444
*/
45-
public function head($url, array $headers = null)
45+
public function head($url, array $headers = [])
4646
{
47-
$opt[CURLOPT_NOBODY] = true;
48-
if ($headers !== null) {
49-
$opt[CURLOPT_HTTPHEADER] = $headers;
50-
}
51-
return $this->exec($url, $opt);
47+
return $this->executor->execute(
48+
$url,
49+
[
50+
CURLOPT_NOBODY => true,
51+
CURLOPT_HTTPHEADER => $headers,
52+
]
53+
);
5254
}
5355

5456
/**
5557
* HTTP POST
56-
* @param string $url Goes to curl_init()
57-
* @param string|array $data Same as CURLOPT_POSTFIELDS
58-
* @param array $headers Same as CURLOPT_HEADER
58+
* @param string $url Goes to curl_init()
59+
* @param string|array $data Same as CURLOPT_POSTFIELDS
60+
* @param array $headers Same as CURLOPT_HEADER
5961
* @return HttpResponse
6062
*/
61-
public function post($url, $data = null, array $headers = null)
63+
public function post($url, $data = '', array $headers = [])
6264
{
63-
$opt[CURLOPT_POST] = true;
64-
if ($data !== null) {
65-
$opt[CURLOPT_POSTFIELDS] = $data;
66-
}
67-
if ($headers !== null) {
68-
$opt[CURLOPT_HTTPHEADER] = $headers;
69-
}
70-
return $this->exec($url, $opt);
65+
return $this->executor->execute(
66+
$url,
67+
[
68+
CURLOPT_POST => true,
69+
CURLOPT_HTTPHEADER => $headers,
70+
CURLOPT_POSTFIELDS => $data,
71+
]
72+
);
7173
}
7274

7375
/**
7476
* HTTP PUT
75-
* @param string $url Goes to curl_init()
76-
* @param string|array $data Same as CURLOPT_POSTFIELDS
77-
* @param array $headers Same as CURLOPT_HEADER
77+
* @param string $url Goes to curl_init()
78+
* @param string|array $data Same as CURLOPT_POSTFIELDS
79+
* @param array $headers Same as CURLOPT_HEADER
7880
* @return HttpResponse
7981
*/
80-
public function put($url, $data = null, array $headers = null)
82+
public function put($url, $data = '', array $headers = [])
8183
{
82-
return $this->request('PUT', $url, $data, $headers);
84+
return $this->executor->execute(
85+
$url,
86+
[
87+
CURLOPT_CUSTOMREQUEST => 'PUT',
88+
CURLOPT_HTTPHEADER => $headers,
89+
CURLOPT_POSTFIELDS => $data,
90+
]
91+
);
8392
}
8493

8594
/**
8695
* HTTP DELETE
87-
* @param string $url Goes to curl_init()
88-
* @param array $headers Same as CURLOPT_HEADER
89-
* @return HttpResponse
90-
*/
91-
public function delete($url, array $headers = null)
92-
{
93-
return $this->request('DELETE', $url, null, $headers);
94-
}
95-
96-
/**
97-
* Custom HTTP method
98-
* @param string $method Goes to CURLOPT_CUSTOMREQUEST
99-
* @param string $url Goes to curl_init()
100-
* @param string|array $data Goes to CURLOPT_POSTFIELDS
101-
* @param array $headers Goes to CURLOPT_HEADER
102-
* @return HttpResponse
103-
*/
104-
public function request($method, $url, $data = null, array $headers = null)
105-
{
106-
$opt[CURLOPT_CUSTOMREQUEST] = $method;
107-
if ($headers !== null) {
108-
$opt[CURLOPT_HTTPHEADER] = $headers;
109-
}
110-
if ($data !== null) {
111-
$opt[CURLOPT_POSTFIELDS] = $data;
112-
}
113-
return $this->exec($url, $opt);
114-
}
115-
116-
/**
117-
* Set additional CURL options to pass with each request
118-
* @param array $userOptions Format is the same as curl_setopt_array().
119-
* Pass an empty array to clear.
120-
*/
121-
public function setOptions(array $userOptions)
122-
{
123-
$this->userOptions = $userOptions;
124-
}
125-
126-
/**
127-
* Init curl with $url, set $options, execute, return response
128-
* @param string $url Goes to curl_init()
129-
* @param array $options Goes to curl_setopt()
130-
* @param Curl $curl
96+
* @param string $url Goes to curl_init()
97+
* @param array $headers Same as CURLOPT_HEADER
13198
* @return HttpResponse
13299
*/
133-
public function exec($url, array $options, Curl $curl = null)
100+
public function delete($url, array $headers = [])
134101
{
135-
$options[CURLOPT_RETURNTRANSFER] = true;
136-
$options[CURLOPT_HEADER] = true;
137-
138-
$curl = $curl ?: new Curl();
139-
$curl->init($url);
140-
$curl->setOptArray(array_replace_recursive(
141-
$this->userOptions,
142-
$options
143-
));
144-
145-
$response = $curl->exec($this->attempts, self::USE_EXCEPTIONS);
146-
147-
$info = $curl->getInfo();
148-
$code = $info['http_code'];
149-
$headerSize = $info['header_size'];
150-
$headers = substr($response, 0, $headerSize);
151-
$headersArray = preg_split("/\r\n/", $headers, -1, PREG_SPLIT_NO_EMPTY);
152-
$body = substr($response, $headerSize);
153-
return new HttpResponse($code, $headersArray, $body);
102+
return $this->executor->execute(
103+
$url,
104+
[
105+
CURLOPT_CUSTOMREQUEST => 'DELETE',
106+
CURLOPT_HTTPHEADER => $headers,
107+
]
108+
);
154109
}
155110
}

src/HttpClientInterface.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace PHPCurl\CurlHttp;
3+
4+
interface HttpClientInterface
5+
{
6+
/**
7+
* HTTP GET
8+
* @param string $url Goes to curl_init()
9+
* @param array $headers Same as CURLOPT_HEADER
10+
* @return HttpResponse
11+
*/
12+
public function get($url, array $headers = []);
13+
14+
/**
15+
* HTTP HEAD (implemented using CURLOPT_NOBODY)
16+
* @param string $url Goes to curl_init()
17+
* @param array $headers Same as CURLOPT_HEADER
18+
* @return HttpResponse
19+
*/
20+
public function head($url, array $headers = []);
21+
22+
/**
23+
* HTTP POST
24+
* @param string $url Goes to curl_init()
25+
* @param string|array $data Same as CURLOPT_POSTFIELDS
26+
* @param array $headers Same as CURLOPT_HEADER
27+
* @return HttpResponse
28+
*/
29+
public function post($url, $data = '', array $headers = []);
30+
31+
/**
32+
* HTTP PUT
33+
* @param string $url Goes to curl_init()
34+
* @param string|array $data Same as CURLOPT_POSTFIELDS
35+
* @param array $headers Same as CURLOPT_HEADER
36+
* @return HttpResponse
37+
*/
38+
public function put($url, $data = '', array $headers = []);
39+
40+
/**
41+
* HTTP DELETE
42+
* @param string $url Goes to curl_init()
43+
* @param array $headers Same as CURLOPT_HEADER
44+
* @return HttpResponse
45+
*/
46+
public function delete($url, array $headers = []);
47+
}

0 commit comments

Comments
 (0)