Skip to content

Commit bcd7811

Browse files
doc
1 parent 74218ed commit bcd7811

File tree

4 files changed

+156
-8
lines changed

4 files changed

+156
-8
lines changed

README.md

Lines changed: 150 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,154 @@
55
[![Code Coverage](https://scrutinizer-ci.com/g/php-guard/curl/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/php-guard/curl/?branch=master)
66
[![GPL Licence](https://badges.frapsoft.com/os/gpl/gpl.png?v=103)](https://opensource.org/licenses/GPL-3.0/)
77

8-
Installation
98

10-
This library is an alternative to "https://github.com/php-curl-class/php-curl-class". It implements classes representing Curl queries and their response that can more easily be manipulated and extended.
9+
This library is an alternative to "https://github.com/php-curl-class/php-curl-class".
10+
It uses objects to represent Curl requests and responses that can more easily be used.
11+
12+
### Installation
13+
14+
Install via [Composer](https://getcomposer.org/)
15+
16+
$ composer require php-guard/curl
17+
18+
### Requirements
19+
20+
- php: ^7.1
21+
22+
23+
### Usage
24+
25+
26+
##### Quick Start
27+
28+
require __DIR__ . '/vendor/autoload.php';
29+
30+
use \PhpGuard\Curl\Curl;
31+
use \PhpGuard\Curl\CurlError;
32+
33+
$this->curl = new Curl();
34+
35+
try {
36+
// Execute a single request
37+
$response = $this->curl->get('http://example.com'); // Return a CurlResponse
38+
echo $response->raw();
39+
40+
// Execute multiple requests
41+
$responses = $this->curl->multi() // Create a MultiCurl object
42+
->get('http://example.com') // Add a get request
43+
->post('http://example.com') // Add a post request
44+
->execute(); // Return an array of CurlResponse
45+
46+
foreach ($responses as $response) {
47+
echo $response->raw();
48+
}
49+
} catch (CurlError $e) {
50+
echo 'Error: ' . $curl->getCode(). ': ' . $curl->getMessage(). "\n";
51+
}
52+
53+
##### Available Methods
54+
55+
**\PhpGuard\Curl\Curl**
56+
57+
HTTP methods
58+
59+
get(string $url, $query = null, array $headers = []): CurlResponse
60+
post(string $url, $data = null, $query = null, array $headers = []): CurlResponse
61+
put(string $url, $data = null, $query = null, array $headers = []): CurlResponse
62+
patch(string $url, $data = null, $query = null, array $headers = []): CurlResponse
63+
delete(string $url, $data = null, $query = null, array $headers = []): CurlResponse
64+
65+
Run a single request
66+
67+
execute(CurlRequest $request): CurlResponse
68+
69+
Prepare multiple requests
70+
71+
multi(array $options = []): MultiCurl
72+
73+
Edit request and response processing (advanced usage)
74+
75+
getCurlRequestFactory(): CurlRequestFactory
76+
getRequestModifierPipeline(): RequestModifierPipeline
77+
78+
79+
**\PhpGuard\Curl\CurlRequest**
80+
81+
You can use Curl to execute requests or you can use
82+
the CurlRequestFactory to return CurlRequest instances.
83+
84+
Properties url, method and data have getters and setters.
85+
86+
The method `setHeaderContentType(string $contentType)` is a shortcut for
87+
88+
$curlRequest->getHeaders['Content-Type'] = $contentType
89+
90+
Other methods
91+
92+
execute(bool $throwExceptionOnHttpError = false): CurlResponse
93+
getCurlOptions(): CurlOptions
94+
getHeaders(): Headers
95+
96+
**\PhpGuard\Curl\MultiCurl**
97+
98+
get(string $url, $query = null, array $headers = []): self
99+
post(string $url, $data = null, $query = null, array $headers = []): self
100+
put(string $url, $data = null, $query = null, array $headers = []): self
101+
patch(string $url, $data = null, $query = null, array $headers = []): self
102+
delete(string $url, $data = null, $query = null, array $headers = []): self
103+
execute(): CurlResponse[]
104+
105+
**\PhpGuard\Curl\CurlResponse**
106+
107+
statusCode(): int
108+
isError(): bool // True if status code >= 300
109+
headers(): Headers
110+
raw() // Raw content of the response
111+
json() // Array or false if not a json response
112+
113+
**\PhpGuard\Curl\CurlRequestFactory**
114+
115+
Set the base url for all requests
116+
117+
setBaseUrl(?string $baseUrl): self
118+
119+
Set the option curl CURLOPT_SSL_VERIFYPEER
120+
121+
setSslVerifyPeer(bool $value): self
122+
123+
Create a CurlRequest
124+
125+
create(string $method, string $url, $data = null, $query = null, array $headers = []): CurlRequest
126+
127+
Edit other default curl options
128+
129+
getDefaultCurlOptions(): CurlOptions
130+
getDefaultHeaders(): Headers
131+
132+
**\PhpGuard\Curl\Collection\CurlOptions**
133+
134+
This class implements `\ArrayAccess`. It can therefore be used as an array.
135+
136+
**\PhpGuard\Curl\Collection\Headers**
137+
138+
This class implements `\ArrayAccess`. It can therefore be used as an array.
139+
140+
In addition, the keys are case insensitive.
141+
142+
**\PhpGuard\Curl\RequestModifierPipeline**
143+
144+
Add an object to modify CURL requests
145+
146+
pipe(RequestModifierInterface $requestModifier): self
147+
148+
By default, FileRequestModifier et PlainTextRequestModifier are active.
149+
If necessary, you can add an instance of ProxyRequestModifier
150+
151+
* ProxyRequestModifier It allows to define curl options to use a proxy
152+
153+
* FileRequestModifier is used to manage file paths starting with @
154+
and passed as a parameter by transforming them into CurlFile
155+
and then modifying the HTTP Content-Type header.
156+
157+
* PlainTextRequestModifier changes the HTTP Content-Type header
158+
to text/plain when a string is passed as a parameter.

src/Curl.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function get(string $url, $query = null, array $headers = []): CurlRespon
6363
*
6464
* @throws CurlError
6565
*/
66-
public function post(string $url, $data = null, $query = null, array $headers = [])
66+
public function post(string $url, $data = null, $query = null, array $headers = []): CurlResponse
6767
{
6868
return $this->curlRequestFactory->create('POST', $url, $data, $query, $headers)->execute();
6969
}
@@ -78,7 +78,7 @@ public function post(string $url, $data = null, $query = null, array $headers =
7878
*
7979
* @throws CurlError
8080
*/
81-
public function put(string $url, $data = null, $query = null, array $headers = [])
81+
public function put(string $url, $data = null, $query = null, array $headers = []): CurlResponse
8282
{
8383
return $this->curlRequestFactory->create('PUT', $url, $data, $query, $headers)->execute();
8484
}
@@ -93,7 +93,7 @@ public function put(string $url, $data = null, $query = null, array $headers = [
9393
*
9494
* @throws CurlError
9595
*/
96-
public function patch(string $url, $data = null, $query = null, array $headers = [])
96+
public function patch(string $url, $data = null, $query = null, array $headers = []): CurlResponse
9797
{
9898
return $this->curlRequestFactory->create('PATCH', $url, $data, $query, $headers)->execute();
9999
}
@@ -108,7 +108,7 @@ public function patch(string $url, $data = null, $query = null, array $headers =
108108
*
109109
* @throws CurlError
110110
*/
111-
public function delete(string $url, $data = null, $query = null, array $headers = [])
111+
public function delete(string $url, $data = null, $query = null, array $headers = []): CurlResponse
112112
{
113113
return $this->curlRequestFactory->create('DELETE', $url, $data, $query, $headers)->execute();
114114
}

src/CurlRequestFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function __construct(Curl $curl, ?string $host = null)
5757
$this->curl = $curl;
5858
}
5959

60-
public function create(string $method, string $url, $data = null, $query = null, array $headers = [])
60+
public function create(string $method, string $url, $data = null, $query = null, array $headers = []): CurlRequest
6161
{
6262
if ($this->baseUrl && is_null(parse_url($url, PHP_URL_HOST))) {
6363
if (isset($url[0]) && '/' != $url[0]) {

src/MultiCurl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function delete(string $url, $data = null, $query = null, array $headers
122122
*
123123
* @throws CurlError
124124
*/
125-
public function execute()
125+
public function execute(): array
126126
{
127127
$mh = curl_multi_init();
128128

0 commit comments

Comments
 (0)