|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class CurlClient |
| 4 | + * |
| 5 | + * @filesource CurlClient.php |
| 6 | + * @created 21.10.2017 |
| 7 | + * @package chillerlan\HTTP |
| 8 | + * @author Smiley <smiley@chillerlan.net> |
| 9 | + * @copyright 2017 Smiley |
| 10 | + * @license MIT |
| 11 | + */ |
| 12 | + |
| 13 | +namespace chillerlan\HTTP; |
| 14 | + |
| 15 | +use chillerlan\Traits\ContainerInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * @property resource $http |
| 19 | + */ |
| 20 | +class CurlClient extends HTTPClientAbstract{ |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \stdClass |
| 24 | + */ |
| 25 | + protected $responseHeaders; |
| 26 | + |
| 27 | + /** @inheritdoc */ |
| 28 | + public function __construct(ContainerInterface $options){ |
| 29 | + parent::__construct($options); |
| 30 | + |
| 31 | + if(!isset($this->options->ca_info) || !is_file($this->options->ca_info)){ |
| 32 | + throw new HTTPClientException('invalid CA file'); |
| 33 | + } |
| 34 | + |
| 35 | + $this->http = curl_init(); |
| 36 | + |
| 37 | + curl_setopt_array($this->http, $this->options->curl_options); |
| 38 | + |
| 39 | + curl_setopt_array($this->http, [ |
| 40 | + CURLOPT_HEADER => false, |
| 41 | + CURLOPT_RETURNTRANSFER => true, |
| 42 | + CURLOPT_PROTOCOLS => CURLPROTO_HTTP|CURLPROTO_HTTPS, |
| 43 | + CURLOPT_CAINFO => $this->options->ca_info, |
| 44 | + CURLOPT_SSL_VERIFYPEER => true, |
| 45 | + CURLOPT_SSL_VERIFYHOST => 2, |
| 46 | + CURLOPT_TIMEOUT => 5, |
| 47 | + CURLOPT_USERAGENT => $this->options->user_agent, |
| 48 | + ]); |
| 49 | + |
| 50 | + } |
| 51 | + |
| 52 | + /** @inheritdoc */ |
| 53 | + public function request(string $url, array $params = null, string $method = null, $body = null, array $headers = null):HTTPResponse{ |
| 54 | + $this->responseHeaders = new \stdClass; |
| 55 | + |
| 56 | + try{ |
| 57 | + $parsedURL = parse_url($url); |
| 58 | + |
| 59 | + if(!isset($parsedURL['host']) || $parsedURL['scheme'] !== 'https'){ |
| 60 | + trigger_error('invalid URL'); |
| 61 | + } |
| 62 | + |
| 63 | + $method = strtoupper($method ?? 'POST'); |
| 64 | + $headers = $this->normalizeRequestHeaders($headers ?? []); |
| 65 | + $options = [CURLOPT_CUSTOMREQUEST => $method]; |
| 66 | + |
| 67 | + if(in_array($method, ['PATCH', 'POST', 'PUT', 'DELETE'], true)){ |
| 68 | + |
| 69 | + if($method === 'POST'){ |
| 70 | + $options = [CURLOPT_POST => true]; |
| 71 | + |
| 72 | + if(!isset($headers['Content-type']) && is_array($body)){ |
| 73 | + $headers += ['Content-type: application/x-www-form-urlencoded']; |
| 74 | + $body = http_build_query($body, '', '&', PHP_QUERY_RFC1738); |
| 75 | + } |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + $options += [CURLOPT_POSTFIELDS => $body]; |
| 80 | + } |
| 81 | + |
| 82 | + $headers += [ |
| 83 | + 'Host: '.$parsedURL['host'], |
| 84 | + 'Connection: close', |
| 85 | + ]; |
| 86 | + |
| 87 | + $params = $params ?? []; |
| 88 | + $url = $url.(!empty($params) ? '?'.http_build_query($params) : ''); |
| 89 | + |
| 90 | + $options += [ |
| 91 | + CURLOPT_URL => $url, |
| 92 | + CURLOPT_HTTPHEADER => $headers, |
| 93 | + CURLOPT_HEADERFUNCTION => [$this, 'headerLine'], |
| 94 | + ]; |
| 95 | + |
| 96 | + curl_setopt_array($this->http, $options); |
| 97 | + |
| 98 | + $response = curl_exec($this->http); |
| 99 | + |
| 100 | + return new HTTPResponse([ |
| 101 | + 'headers' => $this->responseHeaders, |
| 102 | + 'body' => $response, |
| 103 | + ]); |
| 104 | + |
| 105 | + } |
| 106 | + catch(\Exception $e){ |
| 107 | + throw new HTTPClientException($e->getMessage()); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @param resource $curl |
| 114 | + * @param string $header_line |
| 115 | + * |
| 116 | + * @return int |
| 117 | + * |
| 118 | + * @link http://php.net/manual/function.curl-setopt.php CURLOPT_HEADERFUNCTION |
| 119 | + */ |
| 120 | + protected function headerLine(/** @noinspection PhpUnusedParameterInspection */$curl, $header_line){ |
| 121 | + $header = explode(':', $header_line, 2); |
| 122 | + |
| 123 | + if(count($header) === 2){ |
| 124 | + $this->responseHeaders->{trim(strtolower($header[0]))} = trim($header[1]); |
| 125 | + } |
| 126 | + elseif(substr($header_line, 0, 4) === 'HTTP'){ |
| 127 | + $status = explode(' ', $header_line, 3); |
| 128 | + |
| 129 | + $this->responseHeaders->httpversion = explode('/', $status[0], 2)[1]; |
| 130 | + $this->responseHeaders->statuscode = intval($status[1]); |
| 131 | + $this->responseHeaders->statustext = trim($status[2]); |
| 132 | + } |
| 133 | + |
| 134 | + return strlen($header_line); |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments