|
| 1 | +<?php |
| 2 | +namespace PHPCurl\CurlHttp; |
| 3 | + |
| 4 | +use \PHPCurl\CurlWrapper\Curl; |
| 5 | + |
| 6 | +/** |
| 7 | + * Minimalistic HTTP client |
| 8 | + */ |
| 9 | +class HttpClient |
| 10 | +{ |
| 11 | + const USE_EXCEPTIONS = true; |
| 12 | + |
| 13 | + /** |
| 14 | + * Numer of attempts to make per each call |
| 15 | + * @var int |
| 16 | + */ |
| 17 | + protected $attempts = 1; |
| 18 | + |
| 19 | + /** |
| 20 | + * @var array |
| 21 | + */ |
| 22 | + private $userOptions = array(); |
| 23 | + |
| 24 | + /** |
| 25 | + * HTTP GET |
| 26 | + * @param string $url Goes to curl_init() |
| 27 | + * @param array $headers Same as CURLOPT_HEADER |
| 28 | + * @return HttpResponse |
| 29 | + */ |
| 30 | + public function get($url, array $headers = null) |
| 31 | + { |
| 32 | + $opt = array(); |
| 33 | + if ($headers) { |
| 34 | + $opt[CURLOPT_HTTPHEADER] = $headers; |
| 35 | + } |
| 36 | + return $this->exec($url, $opt); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * HTTP HEAD (implemented using CURLOPT_NOBODY) |
| 41 | + * @param string $url Goes to curl_init() |
| 42 | + * @param array $headers Same as CURLOPT_HEADER |
| 43 | + * @return HttpResponse |
| 44 | + */ |
| 45 | + public function head($url, array $headers = null) |
| 46 | + { |
| 47 | + $opt[CURLOPT_NOBODY] = true; |
| 48 | + if ($headers !== null) { |
| 49 | + $opt[CURLOPT_HTTPHEADER] = $headers; |
| 50 | + } |
| 51 | + return $this->exec($url, $opt); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * HTTP POST |
| 56 | + * @param string $url Goes to curl_init() |
| 57 | + * @param string|array $data Same as CURLOPT_POSTFIELDS |
| 58 | + * @param array $headers Same as CURLOPT_HEADER |
| 59 | + * @return HttpResponse |
| 60 | + */ |
| 61 | + public function post($url, $data = null, array $headers = null) |
| 62 | + { |
| 63 | + $opt[CURLOPT_POST] = true; |
| 64 | + if ($data !== null) { |
| 65 | + $opt[CURLOPT_POSTFIELDS] = $data; |
| 66 | + } |
| 67 | + if ($headers !== null) { |
| 68 | + $opt[CURLOPT_HTTPHEADER] = $headers; |
| 69 | + } |
| 70 | + return $this->exec($url, $opt); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * HTTP PUT |
| 75 | + * @param string $url Goes to curl_init() |
| 76 | + * @param string|array $data Same as CURLOPT_POSTFIELDS |
| 77 | + * @param array $headers Same as CURLOPT_HEADER |
| 78 | + * @return HttpResponse |
| 79 | + */ |
| 80 | + public function put($url, $data = null, array $headers = null) |
| 81 | + { |
| 82 | + return $this->request('PUT', $url, $data, $headers); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * HTTP DELETE |
| 87 | + * @param string $url Goes to curl_init() |
| 88 | + * @param array $headers Same as CURLOPT_HEADER |
| 89 | + * @return HttpResponse |
| 90 | + */ |
| 91 | + public function delete($url, array $headers = null) |
| 92 | + { |
| 93 | + return $this->request('DELETE', $url, null, $headers); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Custom HTTP method |
| 98 | + * @param string $method Goes to CURLOPT_CUSTOMREQUEST |
| 99 | + * @param string $url Goes to curl_init() |
| 100 | + * @param string|array $data Goes to CURLOPT_POSTFIELDS |
| 101 | + * @param array $headers Goes to CURLOPT_HEADER |
| 102 | + * @return HttpResponse |
| 103 | + */ |
| 104 | + public function request($method, $url, $data = null, array $headers = null) |
| 105 | + { |
| 106 | + $opt[CURLOPT_CUSTOMREQUEST] = $method; |
| 107 | + if ($headers !== null) { |
| 108 | + $opt[CURLOPT_HTTPHEADER] = $headers; |
| 109 | + } |
| 110 | + if ($data !== null) { |
| 111 | + $opt[CURLOPT_POSTFIELDS] = $data; |
| 112 | + } |
| 113 | + return $this->exec($url, $opt); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Set additional CURL options to pass with each request |
| 118 | + * @param array $userOptions Format is the same as curl_setopt_array(). |
| 119 | + * Pass an empty array to clear. |
| 120 | + */ |
| 121 | + public function setOptions(array $userOptions) |
| 122 | + { |
| 123 | + $this->userOptions = $userOptions; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Init curl with $url, set $options, execute, return response |
| 128 | + * @param string $url Goes to curl_init() |
| 129 | + * @param array $options Goes to curl_setopt() |
| 130 | + * @param Curl $curl |
| 131 | + * @return HttpResponse |
| 132 | + */ |
| 133 | + public function exec($url, array $options, Curl $curl = null) |
| 134 | + { |
| 135 | + $options[CURLOPT_RETURNTRANSFER] = true; |
| 136 | + $options[CURLOPT_HEADER] = true; |
| 137 | + |
| 138 | + $curl = $curl ?: new Curl(); |
| 139 | + $curl->init($url); |
| 140 | + $curl->setOptArray(array_replace_recursive( |
| 141 | + $this->userOptions, |
| 142 | + $options |
| 143 | + )); |
| 144 | + |
| 145 | + $response = $curl->exec($this->attempts, self::USE_EXCEPTIONS); |
| 146 | + |
| 147 | + $info = $curl->getInfo(); |
| 148 | + $code = $info['http_code']; |
| 149 | + $headerSize = $info['header_size']; |
| 150 | + $headers = substr($response, 0, $headerSize); |
| 151 | + $headersArray = preg_split("/\r\n/", $headers, -1, PREG_SPLIT_NO_EMPTY); |
| 152 | + $body = substr($response, $headerSize); |
| 153 | + return new HttpResponse($code, $headersArray, $body); |
| 154 | + } |
| 155 | +} |
0 commit comments