MAPG-43 add classes to handle http queries

remove romanpitak/php-rest-client from composer.json
This commit is contained in:
Bence Pőcze 2020-05-28 21:00:15 +02:00
parent 5738be5c98
commit d1c28e2d91
4 changed files with 119 additions and 48 deletions

View File

@ -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": {

47
composer.lock generated
View File

@ -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",

93
src/Http/Request.php Normal file
View File

@ -0,0 +1,93 @@
<?php namespace MapGuesser\Http;
class Request
{
const HTTP_GET = 0;
const HTTP_POST = 1;
private string $url;
private int $method;
private string $query = '';
private array $headers = [];
public function __construct(string $url, int $method = self::HTTP_GET)
{
$this->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);
}
}

24
src/Http/Response.php Normal file
View File

@ -0,0 +1,24 @@
<?php namespace MapGuesser\Http;
class Response
{
private string $body;
private array $headers;
public function __construct(string $body, array $headers)
{
$this->body = $body;
$this->headers = $headers;
}
public function getBody()
{
return $this->body;
}
public function getHeaders()
{
return $this->headers;
}
}