|
1 | 1 | <?php
|
| 2 | +/* |
| 3 | +Copyright (c) 2017 Jorge Matricali <jorgematricali@gmail.com> |
| 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 |
| 13 | +all 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 |
| 21 | +THE SOFTWARE. |
| 22 | +*/ |
2 | 23 |
|
3 | 24 | namespace Matricali\Http;
|
4 | 25 |
|
5 | 26 | use Psr\Http\Message\RequestInterface;
|
6 | 27 | use Psr\Http\Message\ResponseInterface;
|
7 | 28 |
|
| 29 | +use Matricali\Http\Client\Exception as ClientException; |
| 30 | + |
8 | 31 | class Client
|
9 | 32 | {
|
| 33 | + const VERSION = '1.1'; |
| 34 | + |
10 | 35 | protected $handle = null;
|
| 36 | + protected $responseHeader = ''; |
| 37 | + protected $responseHeaders = []; |
11 | 38 |
|
12 |
| - public function sendRequest(RequestInterface $request): ResponseInterface |
| 39 | + public function __construct() |
13 | 40 | {
|
14 |
| - if ($this->handle === null) { |
15 |
| - $this->handle = curl_init(); |
| 41 | + $this->handle = curl_init(); |
| 42 | + |
| 43 | + if (!is_resource($this->handle)) { |
| 44 | + throw new ClientException(curl_error($this->handle), 'curl'); |
16 | 45 | }
|
17 | 46 |
|
| 47 | + curl_setopt_array($this->handle, [ |
| 48 | + CURLOPT_RETURNTRANSFER => true, |
| 49 | + CURLOPT_AUTOREFERER => true, |
| 50 | + CURLOPT_FOLLOWLOCATION => true, |
| 51 | + CURLOPT_MAXREDIRS => 20, |
| 52 | + CURLOPT_HEADER => false, |
| 53 | + CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, |
| 54 | + CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, |
| 55 | + CURLOPT_USERAGENT => 'jorge-matricali/php-http-client HTTP/' . self::VERSION . ' (libcurl)', |
| 56 | + CURLOPT_CONNECTTIMEOUT => 30, |
| 57 | + CURLOPT_TIMEOUT => 30, |
| 58 | + ]); |
| 59 | + } |
| 60 | + |
| 61 | + public function __destruct() |
| 62 | + { |
| 63 | + curl_close($this->handle); |
| 64 | + } |
| 65 | + |
| 66 | + public function headerFunction($handler, $line) |
| 67 | + { |
| 68 | + $this->responseHeader .= $line; |
| 69 | + return strlen($line); |
| 70 | + } |
| 71 | + |
| 72 | + public function __clone() |
| 73 | + { |
| 74 | + $client = new self; |
| 75 | + $client->handle = curl_copy_handle($this->handle); |
| 76 | + return $client; |
| 77 | + } |
| 78 | + |
| 79 | + protected function parseHeaders($lines) |
| 80 | + { |
| 81 | + if (empty($lines)) { |
| 82 | + return []; |
| 83 | + } |
| 84 | + if (is_string($lines)) { |
| 85 | + $lines = array_filter(explode("\r\n", $lines)); |
| 86 | + } elseif (!is_array($lines)) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + $status = []; |
| 90 | + if (preg_match('%^HTTP/(\d(?:\.\d)?)\s+(\d{3})\s?+(.+)?$%i', $lines[0], $status)) { |
| 91 | + $this->status = array_shift($lines); |
| 92 | + $this->version = $lines[1]; |
| 93 | + $this->statusCode = intval($lines[2]); |
| 94 | + $this->statusMessage = isset($lines[3]) ? $lines[3] : ''; |
| 95 | + } |
| 96 | + |
| 97 | + foreach ($lines as $field) { |
| 98 | + if (!is_array($field)) { |
| 99 | + $field = array_map('trim', explode(':', $field, 2)); |
| 100 | + } |
| 101 | + if (count($field) == 2) { |
| 102 | + $this->responseHeaders[$field[0]] = $field[1]; |
| 103 | + } |
| 104 | + } |
| 105 | + return $this->responseHeaders; |
| 106 | + } |
| 107 | + |
| 108 | + public function sendRequest(RequestInterface $request): ResponseInterface |
| 109 | + { |
| 110 | + curl_setopt($this->handle, CURLOPT_URL, (string) $request->getUri()); |
| 111 | + curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, [$this, 'headerFunction']); |
| 112 | + |
18 | 113 | $ret = curl_exec($this->handle);
|
| 114 | + |
| 115 | + if ($errno = curl_errno($this->handle)) { |
| 116 | + throw new ClientException(curl_error($this->handle), $errno); |
| 117 | + } |
| 118 | + |
| 119 | + $this->parseHeaders($this->responseHeader); |
| 120 | + |
| 121 | + $response = new Response($ret, $this->statusCode, $this->responseHeaders); |
| 122 | + return $response; |
19 | 123 | }
|
20 | 124 |
|
21 | 125 | public function get($uri, $headers = []): ResponseInterface
|
22 | 126 | {
|
23 | 127 | $request = new Request('GET', $uri, $headers);
|
| 128 | + print_r($request); |
24 | 129 | return $this->sendRequest($request);
|
25 | 130 | }
|
26 | 131 | }
|
0 commit comments