From 05d536cc75839ed4b7c8106e0883b63eaf34bf34 Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Fri, 13 Nov 2015 22:01:49 -0600 Subject: [PATCH] Getting a start on making the CURLRequest class functional. --- application/config/services.php | 9 +- system/HTTP/CURLRequest.php | 150 +++++++++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 8 deletions(-) diff --git a/application/config/services.php b/application/config/services.php index 7e2a5daba1..f905fad758 100644 --- a/application/config/services.php +++ b/application/config/services.php @@ -1,6 +1,7 @@ response = $response; + + if (array_key_exists('base_uri', $options)) + { + $this->base_uri = $uri->setURI($options['base_uri']); + } + + $this->parseOptions($options); } //-------------------------------------------------------------------- @@ -53,7 +69,13 @@ class CURLRequest extends Request */ public function request($method, string $url, array $options = []): Response { + $this->parseOptions($options); + $url = $this->prepareURL($url); + + $this->send($url); + + return $this->response; } //-------------------------------------------------------------------- @@ -163,4 +185,124 @@ class CURLRequest extends Request //-------------------------------------------------------------------- + /** + * Sets the correct settings based on the options array + * passed in. + * + * @param array $options + */ + protected function parseOptions(array $options) + { + foreach ($options as $key => $value) + { + if (isset($this->$key)) + { + $this->$key = $value; + } + } + } + + //-------------------------------------------------------------------- + + /** + * If the $url is a relative URL, will attempt to create + * a full URL by prepending $this->base_uri to it. + * + * @param string $url + * + * @return string + */ + protected function prepareURL(string $url): string + { + // If it's a full URI, then we have nothing to do here... + if (strpos($url, '://') !== false) + { + return $url; + } + + return (string)$this->base_uri->resolveRelativeURI($url); + } + + //-------------------------------------------------------------------- + + /** + * Fires the actual cURL request. + * + * @param string $url + */ + public function send(string $url) + { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); + curl_setopt($ch, CURLOPT_HEADER, true); + + // Send the request and wait for a response. + $output = curl_exec($ch); + + if($output === false) + { + echo "Error Number:".curl_errno($ch)."
"; + echo "Error String:".curl_error($ch); + } + + curl_close($ch); + + // Split out our headers and body + $break = strpos($output, "\r\n\r\n"); + + if ($break !== false) + { + // Our headers + $headers = explode("\n", substr($output, 0, $break)); + + $this->setResponseHeaders($headers); + + // Our body + $body = substr($output, $break+4); + $this->response->setBody($body); + } + + return true; + } + + //-------------------------------------------------------------------- + + /** + * Parses the header retrieved from the cURL response into + * our Response object. + * + * @param array $headers + */ + protected function setResponseHeaders(array $headers = []) + { + foreach ($headers as $header) + { + if (($pos = strpos($header, ':')) !== false) + { + $title = substr($header, 0, $pos); + $value = substr($header, $pos+1); + + $this->response->setHeader($title, $value); + } + else if (substr($header, 0, 4) == 'HTTP') + { + preg_match('#^HTTP\/(1\.[01]) ([0-9]+) (.+)#', $header, $matches); + + if (isset($matches[1])) + { + $this->response->setProtocolVersion($matches[1]); + } + + if (isset($matches[2])) + { + $this->response->setStatusCode($matches[2], isset($matches[3]) ? $matches[3] : null); + } + } + } + } + + //-------------------------------------------------------------------- + }