Serializer
5 years ago
Curl.php
5 years ago
Encoder.php
4 years ago
Environment.php
5 years ago
HttpClient.php
4 years ago
HttpException.php
4 years ago
HttpRequest.php
5 years ago
HttpResponse.php
4 years ago
IOException.php
5 years ago
Injector.php
4 years ago
Serializer.php
5 years ago
HttpClient.php
239 lines
| 1 | <?php |
| 2 | |
| 3 | namespace PayPalHttp; |
| 4 | |
| 5 | /** |
| 6 | * Class HttpClient |
| 7 | * @package PayPalHttp |
| 8 | * |
| 9 | * Client used to make HTTP requests. |
| 10 | */ |
| 11 | class HttpClient |
| 12 | { |
| 13 | /** |
| 14 | * @var Environment |
| 15 | */ |
| 16 | public $environment; |
| 17 | |
| 18 | /** |
| 19 | * @var Injector[] |
| 20 | */ |
| 21 | public $injectors = []; |
| 22 | |
| 23 | /** |
| 24 | * @var Encoder |
| 25 | */ |
| 26 | public $encoder; |
| 27 | |
| 28 | /** |
| 29 | * HttpClient constructor. Pass the environment you wish to make calls to. |
| 30 | * |
| 31 | * @param Environment $environment |
| 32 | * @see Environment |
| 33 | */ |
| 34 | function __construct(Environment $environment) |
| 35 | { |
| 36 | $this->environment = $environment; |
| 37 | $this->encoder = new Encoder(); |
| 38 | $this->curlCls = Curl::class; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Injectors are blocks that can be used for executing arbitrary pre-flight logic, such as modifying a request or logging data. |
| 43 | * Executed in first-in first-out order. |
| 44 | * |
| 45 | * @param Injector $inj |
| 46 | */ |
| 47 | public function addInjector(Injector $inj) |
| 48 | { |
| 49 | $this->injectors[] = $inj; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * The method that takes an HTTP request, serializes the request, makes a call to given environment, and deserialize response |
| 54 | * |
| 55 | * @param HttpRequest $httpRequest |
| 56 | * @return HttpResponse |
| 57 | * |
| 58 | * @throws HttpException |
| 59 | * @throws IOException |
| 60 | */ |
| 61 | public function execute(HttpRequest $httpRequest) |
| 62 | { |
| 63 | $requestCpy = clone $httpRequest; |
| 64 | $curl = new Curl(); |
| 65 | |
| 66 | foreach ($this->injectors as $inj) { |
| 67 | $inj->inject($requestCpy); |
| 68 | } |
| 69 | |
| 70 | $url = $this->environment->baseUrl() . $requestCpy->path; |
| 71 | $formattedHeaders = $this->prepareHeaders($requestCpy->headers); |
| 72 | if (!array_key_exists("user-agent", $formattedHeaders)) { |
| 73 | $requestCpy->headers["user-agent"] = $this->userAgent(); |
| 74 | } |
| 75 | |
| 76 | $body = ""; |
| 77 | if (!is_null($requestCpy->body)) { |
| 78 | $rawHeaders = $requestCpy->headers; |
| 79 | $requestCpy->headers = $formattedHeaders; |
| 80 | $body = $this->encoder->serializeRequest($requestCpy); |
| 81 | $requestCpy->headers = $this->mapHeaders($rawHeaders,$requestCpy->headers); |
| 82 | } |
| 83 | |
| 84 | $curl->setOpt(CURLOPT_URL, $url); |
| 85 | $curl->setOpt(CURLOPT_CUSTOMREQUEST, $requestCpy->verb); |
| 86 | $curl->setOpt(CURLOPT_HTTPHEADER, $this->serializeHeaders($requestCpy->headers)); |
| 87 | $curl->setOpt(CURLOPT_RETURNTRANSFER, 1); |
| 88 | $curl->setOpt(CURLOPT_HEADER, 0); |
| 89 | |
| 90 | if (!is_null($requestCpy->body)) { |
| 91 | $curl->setOpt(CURLOPT_POSTFIELDS, $body); |
| 92 | } |
| 93 | |
| 94 | if (strpos($this->environment->baseUrl(), "https://") === 0) { |
| 95 | $curl->setOpt(CURLOPT_SSL_VERIFYPEER, true); |
| 96 | $curl->setOpt(CURLOPT_SSL_VERIFYHOST, 2); |
| 97 | } |
| 98 | |
| 99 | if ($caCertPath = $this->getCACertFilePath()) { |
| 100 | $curl->setOpt(CURLOPT_CAINFO, $caCertPath); |
| 101 | } |
| 102 | |
| 103 | $response = $this->parseResponse($curl); |
| 104 | $curl->close(); |
| 105 | |
| 106 | return $response; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns an array representing headers with their keys |
| 111 | * to be lower case |
| 112 | * @param $headers |
| 113 | * @return array |
| 114 | */ |
| 115 | public function prepareHeaders($headers){ |
| 116 | $preparedHeaders = array_change_key_case($headers); |
| 117 | if (array_key_exists("content-type", $preparedHeaders)) { |
| 118 | $preparedHeaders["content-type"] = strtolower($preparedHeaders["content-type"]); |
| 119 | } |
| 120 | return $preparedHeaders; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns an array representing headers with their key in |
| 125 | * original cases and updated values |
| 126 | * @param $rawHeaders |
| 127 | * @param $formattedHeaders |
| 128 | * @return array |
| 129 | */ |
| 130 | public function mapHeaders($rawHeaders, $formattedHeaders){ |
| 131 | $rawHeadersKey = array_keys($rawHeaders); |
| 132 | foreach ($rawHeadersKey as $array_key) { |
| 133 | if(array_key_exists(strtolower($array_key), $formattedHeaders)){ |
| 134 | $rawHeaders[$array_key] = $formattedHeaders[strtolower($array_key)]; |
| 135 | } |
| 136 | } |
| 137 | return $rawHeaders; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Returns default user-agent |
| 142 | * |
| 143 | * @return string |
| 144 | */ |
| 145 | public function userAgent() |
| 146 | { |
| 147 | return "PayPalHttp-PHP HTTP/1.1"; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Return the filepath to your custom CA Cert if needed. |
| 152 | * @return string |
| 153 | */ |
| 154 | protected function getCACertFilePath() |
| 155 | { |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | protected function setCurl(Curl $curl) |
| 160 | { |
| 161 | $this->curl = $curl; |
| 162 | } |
| 163 | |
| 164 | protected function setEncoder(Encoder $encoder) |
| 165 | { |
| 166 | $this->encoder = $encoder; |
| 167 | } |
| 168 | |
| 169 | private function serializeHeaders($headers) |
| 170 | { |
| 171 | $headerArray = []; |
| 172 | if ($headers) { |
| 173 | foreach ($headers as $key => $val) { |
| 174 | $headerArray[] = $key . ": " . $val; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return $headerArray; |
| 179 | } |
| 180 | |
| 181 | private function parseResponse($curl) |
| 182 | { |
| 183 | $headers = []; |
| 184 | $curl->setOpt(CURLOPT_HEADERFUNCTION, |
| 185 | function($curl, $header) use (&$headers) |
| 186 | { |
| 187 | $len = strlen($header); |
| 188 | |
| 189 | $k = ""; |
| 190 | $v = ""; |
| 191 | |
| 192 | $this->deserializeHeader($header, $k, $v); |
| 193 | $headers[$k] = $v; |
| 194 | |
| 195 | return $len; |
| 196 | }); |
| 197 | |
| 198 | $responseData = $curl->exec(); |
| 199 | $statusCode = $curl->getInfo(CURLINFO_HTTP_CODE); |
| 200 | $errorCode = $curl->errNo(); |
| 201 | $error = $curl->error(); |
| 202 | |
| 203 | if ($errorCode > 0) { |
| 204 | throw new IOException($error, $errorCode); |
| 205 | } |
| 206 | |
| 207 | $body = $responseData; |
| 208 | |
| 209 | if ($statusCode >= 200 && $statusCode < 300) { |
| 210 | $responseBody = NULL; |
| 211 | |
| 212 | if (!empty($body)) { |
| 213 | $responseBody = $this->encoder->deserializeResponse($body, $this->prepareHeaders($headers)); |
| 214 | } |
| 215 | |
| 216 | return new HttpResponse( |
| 217 | $errorCode === 0 ? $statusCode : $errorCode, |
| 218 | $responseBody, |
| 219 | $headers |
| 220 | ); |
| 221 | } else { |
| 222 | throw new HttpException($body, $statusCode, $headers); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private function deserializeHeader($header, &$key, &$value) |
| 227 | { |
| 228 | if (strlen($header) > 0) { |
| 229 | if (empty($header) || strpos($header, ':') === false) { |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | list($k, $v) = explode(":", $header); |
| 234 | $key = trim($k); |
| 235 | $value = trim($v); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 |