Skip to content

Commit 8a077fa

Browse files
committed
Version 1.0.0
0 parents  commit 8a077fa

11 files changed

+551
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Alexey Karapetov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#CurlHttp
2+
The simplest cURL HTTP client for PHP
3+
4+
##Install
5+
Via [composer](https://getcomposer.org):
6+
`$ composer require "phpcurl/curlhttp"`
7+
8+
##Usage
9+
10+
It is really that easy.
11+
12+
```php
13+
<?php
14+
require_once 'vendor/autoload.php';
15+
use PHPCurl\CurlHttp\HttpClient;
16+
17+
$http = new HttpClient();
18+
19+
$http->setOptions(array(
20+
CURLOPT_FOLLOWLOCATION => false, // Any arbitrary curl options you want
21+
));
22+
23+
$response = $http->post('http://example.com/?a=b', 'my post data', ['User-Agent: My php crawler']);
24+
// Supported: get(), post(), head(), post(), put(), delete(), custom methods
25+
26+
$body = $response->getBody(); // Response body, string
27+
28+
/*
29+
30+
<!doctype html>
31+
<html>
32+
...
33+
</html>
34+
35+
*/
36+
37+
$statusCode = $response->getStatus(); // HTTP status, int
38+
39+
/*
40+
41+
200
42+
43+
*/
44+
45+
$headers = $response->getHeaders(); // HTTP response headers, array
46+
/*
47+
48+
array(
49+
0 => 'HTTP/1.1 200 OK',
50+
1 => 'Accept-Ranges: bytes',
51+
2 => 'Cache-Control: max-age=604800',
52+
3 => 'Content-Type: text/html',
53+
4 => 'Date: Wed, 03 Feb 2016 07:01:58 GMT',
54+
5 => 'Etag: "359670651"',
55+
6 => 'Expires: Wed, 10 Feb 2016 07:01:58 GMT',
56+
7 => 'Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT',
57+
8 => 'Server: ECS (rhv/818F)',
58+
9 => 'Vary: Accept-Encoding',
59+
10 => 'X-Cache: HIT',
60+
11 => 'x-ec-custom-error: 1',
61+
12 => 'Content-Length: 1270',
62+
);
63+
64+
*/
65+
```

composer.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "phpcurl/curlhttp",
3+
"type": "library",
4+
"description": "Super simple HTTP client",
5+
"keywords": [
6+
"curl",
7+
"curl_multi",
8+
"curl_share",
9+
"oop",
10+
"wrapper",
11+
"testing",
12+
"dependency",
13+
"injection",
14+
"phpunit",
15+
"testable",
16+
"mock"
17+
],
18+
"license": "MIT",
19+
"authors": [
20+
{
21+
"name": "Alexey Karapetov",
22+
"email": "karapetov@gmail.com",
23+
"role": "Developer"
24+
}
25+
],
26+
"require": {
27+
"php": ">=5.3.0",
28+
"phpcurl/curlwrapper": "^1"
29+
},
30+
"require-dev": {
31+
"phpunit/phpunit": "4.*",
32+
"weew/php-http-server": "^1.0.0"
33+
},
34+
"autoload": {
35+
"psr-4": {
36+
"PHPCurl\\CurlHttp\\": "src/"
37+
}
38+
}
39+
}

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit
3+
bootstrap="vendor/autoload.php"
4+
backupGlobals="true"
5+
backupStaticAttributes="true"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
stopOnFailure="false"
11+
verbose="true"
12+
syntaxCheck="true">
13+
<testsuites>
14+
<testsuite name="Main">
15+
<directory>./test</directory>
16+
</testsuite>
17+
</testsuites>
18+
<filter>
19+
<whitelist>
20+
<directory suffix=".php">./src/</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

src/HttpClient.php

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
<?php
2+
namespace PHPCurl\CurlHttp;
3+
4+
use \PHPCurl\CurlWrapper\Curl;
5+
6+
/**
7+
* Minimalistic HTTP client
8+
*/
9+
class HttpClient
10+
{
11+
const USE_EXCEPTIONS = true;
12+
13+
/**
14+
* Numer of attempts to make per each call
15+
* @var int
16+
*/
17+
protected $attempts = 1;
18+
19+
/**
20+
* @var array
21+
*/
22+
private $userOptions = array();
23+
24+
/**
25+
* HTTP GET
26+
* @param string $url Goes to curl_init()
27+
* @param array $headers Same as CURLOPT_HEADER
28+
* @return HttpResponse
29+
*/
30+
public function get($url, array $headers = null)
31+
{
32+
$opt = array();
33+
if ($headers) {
34+
$opt[CURLOPT_HTTPHEADER] = $headers;
35+
}
36+
return $this->exec($url, $opt);
37+
}
38+
39+
/**
40+
* HTTP HEAD (implemented using CURLOPT_NOBODY)
41+
* @param string $url Goes to curl_init()
42+
* @param array $headers Same as CURLOPT_HEADER
43+
* @return HttpResponse
44+
*/
45+
public function head($url, array $headers = null)
46+
{
47+
$opt[CURLOPT_NOBODY] = true;
48+
if ($headers !== null) {
49+
$opt[CURLOPT_HTTPHEADER] = $headers;
50+
}
51+
return $this->exec($url, $opt);
52+
}
53+
54+
/**
55+
* 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
59+
* @return HttpResponse
60+
*/
61+
public function post($url, $data = null, array $headers = null)
62+
{
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);
71+
}
72+
73+
/**
74+
* 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
78+
* @return HttpResponse
79+
*/
80+
public function put($url, $data = null, array $headers = null)
81+
{
82+
return $this->request('PUT', $url, $data, $headers);
83+
}
84+
85+
/**
86+
* 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
131+
* @return HttpResponse
132+
*/
133+
public function exec($url, array $options, Curl $curl = null)
134+
{
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);
154+
}
155+
}

src/HttpResponse.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
namespace PHPCurl\CurlHttp;
3+
4+
/**
5+
* A simple HTTP response
6+
*/
7+
class HttpResponse
8+
{
9+
/**
10+
* @var int
11+
*/
12+
private $status;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $body;
18+
19+
/**
20+
* @var array
21+
*/
22+
private $headers;
23+
24+
/**
25+
* HttpResponse constructor.
26+
* @param int $status
27+
* @param array $headers
28+
* @param string $body
29+
*/
30+
public function __construct($status, array $headers, $body)
31+
{
32+
$this->status = $status;
33+
$this->body = $body;
34+
$this->headers = $headers;
35+
}
36+
37+
/**
38+
* @return int
39+
*/
40+
public function getStatus()
41+
{
42+
return $this->status;
43+
}
44+
45+
/**
46+
* @return string
47+
*/
48+
public function getBody()
49+
{
50+
return $this->body;
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function getHeaders()
57+
{
58+
return $this->headers;
59+
}
60+
}

0 commit comments

Comments
 (0)