|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Async\Http; |
| 4 | + |
| 5 | +class AsyncHttpClient |
| 6 | +{ |
| 7 | + public function get(string $url, array $headers = []): \Generator |
| 8 | + { |
| 9 | + return $this->request('GET', $url, $headers); |
| 10 | + } |
| 11 | + |
| 12 | + public function post(string $url, array $headers = [], string $body = ''): \Generator |
| 13 | + { |
| 14 | + return $this->request('POST', $url, $headers, $body); |
| 15 | + } |
| 16 | + |
| 17 | + public function request(string $method, string $url, array $headers = [], string $body = ''): \Generator |
| 18 | + { |
| 19 | + $parts = parse_url($url); |
| 20 | + $scheme = $parts['scheme'] ?? 'http'; |
| 21 | + $host = $parts['host'] ?? 'localhost'; |
| 22 | + $port = $scheme === 'https' ? 443 : 80; |
| 23 | + $path = $parts['path'] ?? '/'; |
| 24 | + $query = $parts['query'] ?? ''; |
| 25 | + if ($query) { |
| 26 | + $path .= '?'.$query; |
| 27 | + } |
| 28 | + |
| 29 | + $remote = ($scheme === 'https' ? 'ssl://' : '').$host.':'.$port; |
| 30 | + $errno = $errstr = null; |
| 31 | + |
| 32 | + $fp = stream_socket_client($remote, $errno, $errstr, 5, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT); |
| 33 | + if (! $fp) { |
| 34 | + throw new \Exception("Connection failed: $errstr"); |
| 35 | + } |
| 36 | + |
| 37 | + stream_set_blocking($fp, false); |
| 38 | + |
| 39 | + // Build request headers |
| 40 | + $headerStr = "$method $path HTTP/1.1\r\n"; |
| 41 | + $headerStr .= "Host: $host\r\n"; |
| 42 | + $headerStr .= "Connection: close\r\n"; |
| 43 | + |
| 44 | + foreach ($headers as $k => $v) { |
| 45 | + $headerStr .= "$k: $v\r\n"; |
| 46 | + } |
| 47 | + |
| 48 | + if ($body !== '') { |
| 49 | + $headerStr .= 'Content-Length: '.strlen($body)."\r\n"; |
| 50 | + } |
| 51 | + |
| 52 | + $headerStr .= "\r\n".$body; |
| 53 | + |
| 54 | + fwrite($fp, $headerStr); |
| 55 | + |
| 56 | + // Wait for response |
| 57 | + $read = [$fp]; |
| 58 | + $write = null; |
| 59 | + $except = null; |
| 60 | + if (stream_select($read, $write, $except, 5)) { |
| 61 | + $response = ''; |
| 62 | + while (! feof($fp)) { |
| 63 | + $chunk = fread($fp, 8192); |
| 64 | + if ($chunk === false) { |
| 65 | + break; |
| 66 | + } |
| 67 | + $response .= $chunk; |
| 68 | + } |
| 69 | + fclose($fp); |
| 70 | + |
| 71 | + // Split headers and body |
| 72 | + [$rawHeaders, $body] = explode("\r\n\r\n", $response, 2); |
| 73 | + |
| 74 | + return yield new HttpResponse($rawHeaders, $body); |
| 75 | + } else { |
| 76 | + throw new \Exception('Timeout waiting for response'); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public function put(string $url, array $headers = [], string $body = ''): \Generator |
| 81 | + { |
| 82 | + return $this->request('PUT', $url, $headers, $body); |
| 83 | + } |
| 84 | + |
| 85 | + public function patch(string $url, array $headers = [], string $body = ''): \Generator |
| 86 | + { |
| 87 | + return $this->request('PATCH', $url, $headers, $body); |
| 88 | + } |
| 89 | + |
| 90 | + public function delete(string $url, array $headers = []): \Generator |
| 91 | + { |
| 92 | + return $this->request('DELETE', $url, $headers); |
| 93 | + } |
| 94 | +} |
0 commit comments