diff --git a/composer.json b/composer.json index 6325f0a..5b7d429 100644 --- a/composer.json +++ b/composer.json @@ -4,8 +4,7 @@ "description": "MapGuesser Application", "license": "GNU GPL 3.0", "require": { - "vlucas/phpdotenv": "^4.1", - "romanpitak/php-rest-client": "^1.2" + "vlucas/phpdotenv": "^4.1" }, "require-dev": {}, "autoload": { diff --git a/composer.lock b/composer.lock index fc6069d..4bbcd4f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3778b9431ef3d22705bdbf1653102a1a", + "content-hash": "29431cf83ee884f01ee954b1068c0ccc", "packages": [ { "name": "phpoption/phpoption", @@ -61,51 +61,6 @@ ], "time": "2020-03-21T18:07:53+00:00" }, - { - "name": "romanpitak/php-rest-client", - "version": "v1.2.1", - "source": { - "type": "git", - "url": "https://github.com/romanpitak/PHP-REST-Client.git", - "reference": "728b6c44040a13daeb8033953f5f3cdd3dde1980" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/romanpitak/PHP-REST-Client/zipball/728b6c44040a13daeb8033953f5f3cdd3dde1980", - "reference": "728b6c44040a13daeb8033953f5f3cdd3dde1980", - "shasum": "" - }, - "require": { - "ext-curl": "*", - "php": ">=5.3.3" - }, - "type": "library", - "autoload": { - "psr-4": { - "RestClient\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Roman Piták", - "email": "roman@pitak.net", - "homepage": "http://pitak.net", - "role": "Developer" - } - ], - "description": "REST client library.", - "homepage": "https://github.com/romanpitak/PHP-REST-Client", - "keywords": [ - "client", - "http", - "rest" - ], - "time": "2015-02-19T15:32:16+00:00" - }, { "name": "symfony/polyfill-ctype", "version": "v1.17.0", diff --git a/src/Http/Request.php b/src/Http/Request.php new file mode 100644 index 0000000..2e5426a --- /dev/null +++ b/src/Http/Request.php @@ -0,0 +1,93 @@ +url = $url; + $this->method = $method; + } + + public function setQuery($query) + { + if (is_string($query)) { + $this->query = $query; + } else { + $this->query = http_build_query($query); + } + } + + public function setHeaders(array $headers) + { + $this->headers = array_merge($this->headers, $headers); + } + + public function send(): Response + { + $ch = curl_init(); + + if ($this->method === self::HTTP_GET) { + $url = $this->url . '?' . $this->query; + } elseif ($this->method === self::HTTP_POST) { + $url = $this->url; + + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->query); + } + + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); + curl_setopt($ch, CURLOPT_TIMEOUT, 20); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_USERAGENT, 'MapGuesser cURL/1.0'); + + if (count($this->headers) > 0) { + curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); + } + + $responseHeaders = []; + curl_setopt( + $ch, + CURLOPT_HEADERFUNCTION, + function ($ch, $header) use (&$responseHeaders) { + $len = strlen($header); + $header = explode(':', $header, 2); + + if (count($header) < 2) { + return $len; + } + + $responseHeaders[strtolower(trim($header[0]))][] = trim($header[1]); + + return $len; + } + ); + + $responseBody = curl_exec($ch); + + if ($responseBody === false) { + $error = curl_error($ch); + + curl_close($ch); + + throw new \Exception($error); + } + + curl_close($ch); + + return new Response($responseBody, $responseHeaders); + } +} diff --git a/src/Http/Response.php b/src/Http/Response.php new file mode 100644 index 0000000..9bee447 --- /dev/null +++ b/src/Http/Response.php @@ -0,0 +1,24 @@ +body = $body; + $this->headers = $headers; + } + + public function getBody() + { + return $this->body; + } + + public function getHeaders() + { + return $this->headers; + } +}