first commit.
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
<?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);
|
||||
|
||||
// throw new Exception('Error al realizar la solicitud. '. curl_error($handler));
|
||||
|
||||
curl_close($handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user