|
5 | 5 | [](https://scrutinizer-ci.com/g/php-guard/curl/?branch=master)
|
6 | 6 | [](https://opensource.org/licenses/GPL-3.0/)
|
7 | 7 |
|
8 |
| -Installation |
9 | 8 |
|
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. |
0 commit comments