MAPG-43 add classes to handle http queries
remove romanpitak/php-rest-client from composer.json
This commit is contained in:
parent
5738be5c98
commit
d1c28e2d91
@ -4,8 +4,7 @@
|
|||||||
"description": "MapGuesser Application",
|
"description": "MapGuesser Application",
|
||||||
"license": "GNU GPL 3.0",
|
"license": "GNU GPL 3.0",
|
||||||
"require": {
|
"require": {
|
||||||
"vlucas/phpdotenv": "^4.1",
|
"vlucas/phpdotenv": "^4.1"
|
||||||
"romanpitak/php-rest-client": "^1.2"
|
|
||||||
},
|
},
|
||||||
"require-dev": {},
|
"require-dev": {},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
47
composer.lock
generated
47
composer.lock
generated
@ -4,7 +4,7 @@
|
|||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "3778b9431ef3d22705bdbf1653102a1a",
|
"content-hash": "29431cf83ee884f01ee954b1068c0ccc",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "phpoption/phpoption",
|
"name": "phpoption/phpoption",
|
||||||
@ -61,51 +61,6 @@
|
|||||||
],
|
],
|
||||||
"time": "2020-03-21T18:07:53+00:00"
|
"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",
|
"name": "symfony/polyfill-ctype",
|
||||||
"version": "v1.17.0",
|
"version": "v1.17.0",
|
||||||
|
93
src/Http/Request.php
Normal file
93
src/Http/Request.php
Normal 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
24
src/Http/Response.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user