diff --git a/src/ncc/Abstracts/ExceptionCodes.php b/src/ncc/Abstracts/ExceptionCodes.php index 06b2192..995041b 100644 --- a/src/ncc/Abstracts/ExceptionCodes.php +++ b/src/ncc/Abstracts/ExceptionCodes.php @@ -268,6 +268,11 @@ */ const MissingDependencyException = -1751; + /** + * @see HttpException + */ + const HttpException = -1752; + /** * All the exception codes from NCC */ @@ -322,6 +327,7 @@ self::ComposerNotAvailableException, self::ComposerException, self::UserAbortedOperationException, - self::MissingDependencyException + self::MissingDependencyException, + self::HttpException ]; } \ No newline at end of file diff --git a/src/ncc/Abstracts/HttpRequestType.php b/src/ncc/Abstracts/HttpRequestType.php new file mode 100644 index 0000000..75ba864 --- /dev/null +++ b/src/ncc/Abstracts/HttpRequestType.php @@ -0,0 +1,11 @@ +message = $message; + } + } \ No newline at end of file diff --git a/src/ncc/Objects/HttpRequest.php b/src/ncc/Objects/HttpRequest.php new file mode 100644 index 0000000..fc3760f --- /dev/null +++ b/src/ncc/Objects/HttpRequest.php @@ -0,0 +1,87 @@ +Type = HttpRequestType::GET; + $this->Body = null; + $this->Headers = [ + 'User-Agent: ncc/1.0' + ]; + } + + /** + * Returns an array representation of the object. + * + * @return array + */ + public function toArray(): array + { + return [ + 'type' => $this->Type, + 'url' => $this->Url, + 'headers' => $this->Headers, + 'body' => $this->Body, + 'authentication' => $this->Authentication + ]; + } + + /** + * Constructs a new HttpRequest object from an array representation. + * + * @param array $data + * @return static + */ + public static function fromArray(array $data): self + { + $request = new self(); + $request->Type = $data['type']; + $request->Url = $data['url']; + $request->Headers = $data['headers']; + $request->Body = $data['body']; + $request->Authentication = $data['authentication']; + return $request; + } + } \ No newline at end of file diff --git a/src/ncc/Objects/HttpResponse.php b/src/ncc/Objects/HttpResponse.php new file mode 100644 index 0000000..fe9f9dd --- /dev/null +++ b/src/ncc/Objects/HttpResponse.php @@ -0,0 +1,50 @@ +StatusCode = 0; + $this->Headers = []; + $this->Body = ''; + } + + /** + * Returns an array representation of the object. + * + * @return array + */ + public function toArray(): array + { + return [ + 'status_code' => $this->StatusCode, + 'headers' => $this->Headers, + 'body' => $this->Body + ]; + } + } \ No newline at end of file diff --git a/src/ncc/Utilities/HttpClient.php b/src/ncc/Utilities/HttpClient.php new file mode 100644 index 0000000..3bf119d --- /dev/null +++ b/src/ncc/Utilities/HttpClient.php @@ -0,0 +1,111 @@ +Url); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_HEADER, true); + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($curl, CURLOPT_MAXREDIRS, 10); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + + switch($httpRequest->Type) + { + case HttpRequestType::GET: + curl_setopt($curl, CURLOPT_HTTPGET, true); + break; + + case HttpRequestType::POST: + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $httpRequest->Body); + break; + + case HttpRequestType::PUT: + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); + curl_setopt($curl, CURLOPT_POSTFIELDS, $httpRequest->Body); + break; + + case HttpRequestType::DELETE: + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); + break; + + default: + throw new HttpException(sprintf('Invalid HTTP request type: %s', $httpRequest->Type)); + } + + if (is_array($httpRequest->Authentication)) + { + curl_setopt($curl, CURLOPT_USERPWD, $httpRequest->Authentication[0] . ':' . $httpRequest->Authentication[1]); + } + else if (is_string($httpRequest->Authentication)) + { + curl_setopt($curl, CURLOPT_USERPWD, $httpRequest->Authentication); + } + + if (count($httpRequest->Headers) > 0) + curl_setopt($curl, CURLOPT_HTTPHEADER, $httpRequest->Headers); + + $response = curl_exec($curl); + + if ($response === false) + { + $error = curl_error($curl); + curl_close($curl); + throw new HttpException($error); + } + + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $headers = substr($response, 0, $headerSize); + $body = substr($response, $headerSize); + + $httpResponse = new HttpResponse(); + $httpResponse->StatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + $httpResponse->Headers = self::parseHeaders($headers); + $httpResponse->Body = $body; + + curl_close($curl); + + return $httpResponse; + } + + /** + * Takes the return headers of a cURL request and parses them into an array. + * + * @param string $headers + * @return array + */ + private static function parseHeaders(string $headers): array + { + $headers = explode("\r", $headers); + $headers = array_filter($headers, function ($header) + { + return !empty($header); + }); + $headers = array_map(function ($header) { + return explode(':', $header, 2); + }, $headers); + + return array_combine(array_map(function ($header) { return strtolower($header[0]); }, $headers), + array_map(function ($header) { return trim($header[1]); }, $headers) + ); + } + } \ No newline at end of file