Compare commits

..

No commits in common. "master" and "v0.20" have entirely different histories.

2 changed files with 12 additions and 34 deletions

View File

@ -7,15 +7,13 @@ class Request implements IRequest
{ {
private string $url; private string $url;
private ?string $method = null; private int $method;
private string $query = ''; private string $query = '';
private ?string $body = null;
private array $headers = []; private array $headers = [];
public function __construct(string $url = '', ?string $method = null) public function __construct(string $url = '', int $method = self::HTTP_GET)
{ {
$this->url = $url; $this->url = $url;
$this->method = $method; $this->method = $method;
@ -26,7 +24,7 @@ class Request implements IRequest
$this->url = $url; $this->url = $url;
} }
public function setMethod(string $method): void public function setMethod(int $method): void
{ {
$this->method = $method; $this->method = $method;
} }
@ -40,11 +38,6 @@ class Request implements IRequest
} }
} }
public function setBody(string $body): void
{
$this->body = $body;
}
public function setHeaders(array $headers): void public function setHeaders(array $headers): void
{ {
$this->headers = array_merge($this->headers, $headers); $this->headers = array_merge($this->headers, $headers);
@ -54,20 +47,13 @@ class Request implements IRequest
{ {
$ch = curl_init(); $ch = curl_init();
$url = $this->url . '?' . $this->query; if ($this->method === self::HTTP_POST) {
$url = $this->url;
if ($this->body !== null) { curl_setopt($ch, CURLOPT_POST, 1);
if ($this->method === null) { curl_setopt($ch, CURLOPT_POSTFIELDS, $this->query);
$this->method = self::HTTP_POST; } else {
} $url = $this->url . '?' . $this->query;
if ($this->method === self::HTTP_POST) {
curl_setopt($ch, CURLOPT_POST, 1);
} else {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
} }
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);

View File

@ -2,24 +2,16 @@
interface IRequest interface IRequest
{ {
const HTTP_GET = 'GET'; const HTTP_GET = 0;
const HTTP_POST = 'POST'; const HTTP_POST = 1;
const HTTP_PUT = 'PUT';
const HTTP_PATCH = 'PATCH';
const HTTP_DELETE = 'DELETE';
public function setUrl(string $url): void; public function setUrl(string $url): void;
public function setMethod(string $method): void; public function setMethod(int $method): void;
public function setQuery($query): void; public function setQuery($query): void;
public function setBody(string $body): void;
public function setHeaders(array $headers): void; public function setHeaders(array $headers): void;
public function send(): IResponse; public function send(): IResponse;