53 lines
1.4 KiB
PHP
Executable File
53 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\external_requests;
|
|
|
|
use app\exceptions\GenericErrorException;
|
|
|
|
class CurlRequest
|
|
{
|
|
private string $url;
|
|
private string $request;
|
|
private array $headers;
|
|
private ?array $body;
|
|
private bool $isReturnable;
|
|
|
|
protected function __construct(string $url, string $request, array $headers, ?array $body, bool $isReturnable)
|
|
{
|
|
$this->url = $url;
|
|
$this->request = $request;
|
|
$this->headers = $headers;
|
|
$this->body = $body;
|
|
$this->isReturnable = $isReturnable;
|
|
}
|
|
|
|
/**
|
|
* @throws GenericErrorException
|
|
*/
|
|
public function makeRequest(): string
|
|
{
|
|
$curl = curl_init();
|
|
$this->setCurlBody($curl);
|
|
|
|
$response = curl_exec($curl);
|
|
if(!$response){
|
|
throw new GenericErrorException();
|
|
}
|
|
curl_close($curl);
|
|
|
|
return $response;
|
|
}
|
|
|
|
public function setCurlBody(\CurlHandle &$curl): void
|
|
{
|
|
curl_setopt($curl, CURLOPT_URL, $this->url);
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
|
|
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->request);
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, $this->isReturnable);
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
curl_setopt($curl,CURLOPT_POST, true);
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($this->body));
|
|
}
|
|
} |