Files
duckbrain-toolbox/packages/http/src/Libs/CurlRequest.php
T
kj 0082cbe3f4 fix(http): drop deprecated curl_close calls; resolve Proxy destination lazily
PHP 8.5 deprecates curl_close() (no-op since 8.0) and Libs\Proxy fataled
on class load because SOURCE_URL was evaluated in a static property
initializer. Stamps http with per-component core data for load-verified
clients (aria2, proxy, compreFace).
2026-10-05 17:36:08 -03:00

171 lines
3.7 KiB
PHP

<?php
namespace Libs;
/**
* CurlRequest - DuckBrain
*
* Facade for CURL.
*
* @author KJ
* @website https://kj2.me
* @licence MIT
*/
class CurlRequest
{
public array $headers = [];
public array $options = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
];
public int $statusCode = 0;
protected string $responseHeaders = '';
protected string $responseBody = '';
/**
* Sends the request.
*
* @param array $options
* @return void
*/
protected function send(array $options): void
{
$handler = curl_init();
$headers = [];
foreach ($this->headers as $index => $value) {
$headers[] = "$index: $value";
}
$options[CURLOPT_HTTPHEADER] = $headers;
foreach ($this->options as $index => $option) {
if (!isset($options[$index])) {
$options[$index] = $option;
}
}
curl_setopt_array($handler, $options);
$this->responseBody = curl_exec($handler);
$this->responseHeaders = curl_getinfo($handler, CURLINFO_HEADER_OUT);
$this->statusCode = curl_getinfo($handler, CURLINFO_HTTP_CODE);
}
/**
* Sends the request using the GET method.
*
* @param string $url
* @param array $args
*
* @return CurlRequest
*/
public function get(string $url, array $args = []): CurlRequest
{
$this->send([
CURLOPT_URL => $url . '?' . http_build_query($args),
CURLOPT_POST => false,
CURLOPT_PUT => false,
]);
return $this;
}
/**
* Sends the request using the POST method.
*
* @param string $url
* @param string $fields
*
* @return CurlRequest
*/
public function post(string $url, string $fields = ''): CurlRequest
{
$this->send([
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $fields,
]);
return $this;
}
/**
* Sends the request using the PUT method.
*
* @param string $url
* @param string $fields
*
* @return CurlRequest
*/
public function put(string $url, string $fields = ''): CurlRequest
{
$this->send([
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => $fields,
]);
return $this;
}
/**
* Sends the request using the PATCH method.
*
* @param string $url
* @param string $fields
*
* @return CurlRequest
*/
public function patch(string $url, string $fields = ''): CurlRequest
{
$this->send([
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS => $fields,
]);
return $this;
}
/**
* Sends the request using the DELETE method.
*
* @param string $url
* @param string $fields
*
* @return CurlRequest
*/
public function delete(string $url, string $fields = ''): CurlRequest
{
$this->send([
CURLOPT_URL => $url,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_POSTFIELDS => $fields,
]);
return $this;
}
/**
* Returns the response headers.
*
* @return string
*/
public function headers(): string
{
return $this->responseHeaders;
}
/**
* Returns the response body.
*
* @return string
*/
public function body(): string
{
return $this->responseBody;
}
}