- First commit, heavy WIP
This commit is contained in:
commit
0f9495e07a
6 files changed
+237
No files matched your search
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Api\Transport;
|
||||
|
||||
use Ulmus\Api\Common\MethodEnum;
|
||||
use Ulmus\Attribute\Obj\Method;
|
||||
|
||||
abstract class CurlTransport {
|
||||
|
||||
public $timeout = 1;
|
||||
|
||||
public function post(string $url, array $data) : mixed
|
||||
{
|
||||
return $this->request(MethodEnum::Post, $url, $data);
|
||||
}
|
||||
|
||||
public function get(string $url, array $data) : mixed
|
||||
{
|
||||
return $this->request(MethodEnum::Get, $url, $data);
|
||||
}
|
||||
|
||||
public function delete(string $url, array $data) : mixed
|
||||
{
|
||||
return $this->request(MethodEnum::Delete, $url, $data);
|
||||
}
|
||||
|
||||
public function patch(string $url, array $data) : mixed
|
||||
{
|
||||
return $this->request(MethodEnum::Patch, $url, $data);
|
||||
}
|
||||
|
||||
public function put(string $url, array $data) : mixed
|
||||
{
|
||||
return $this->request(MethodEnum::Put, $url, $data);
|
||||
}
|
||||
|
||||
public function request(MethodEnum $method, string $url, array $data) : mixed
|
||||
{
|
||||
$ch = curl_init();
|
||||
|
||||
$options = [
|
||||
CURLOPT_URL => $url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => $this->headers,
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT_MS => $this->timeout * 200,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
];
|
||||
|
||||
switch ($method) {
|
||||
case MethodEnum::Put:
|
||||
case MethodEnum::Delete:
|
||||
$options[CURLOPT_CUSTOMREQUEST] = $method->value;
|
||||
break;
|
||||
|
||||
case MethodEnum::Post:
|
||||
$options[CURLOPT_POST] = true;
|
||||
$options[CURLOPT_POSTFIELDS] = http_build_query($data, '', '&');
|
||||
break;
|
||||
|
||||
default:
|
||||
case MethodEnum::Get:
|
||||
}
|
||||
|
||||
|
||||
foreach($options as $opt => $value) {
|
||||
curl_setopt($ch, $opt, $value);
|
||||
}
|
||||
|
||||
if ( ( false === curl_exec($ch) ) && $this->throwErrors ) {
|
||||
$errno = curl_errno($ch);
|
||||
$errors = array_filter([ curl_error($ch) , CurlErrors::CURL_CODES[$errno] ?? null ]);
|
||||
|
||||
throw new CurlException(implode(PHP_EOL, $errors), $errno);
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user