Skip to content

Instantly share code, notes, and snippets.

@Domm98CZ

Domm98CZ/CurlService.php

Last active Sep 17, 2020
Embed
What would you like to do?
<?php declare(strict_types=1);
namespace CurlService;
use RuntimeException;
/**
* Class CurlService
* @package CurlService
*/
class CurlService
{
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public const
CURL_METHOD_GET = 'GET',
CURL_METHOD_POST = 'POST',
CURL_METHOD_PUT = 'PUT',
CURL_METHOD_HEAD = 'HEAD',
CURL_METHOD_DELETE = 'DELETE',
CURL_METHOD_PATCH = 'PATCH',
CURL_METHOD_OPTIONS = 'OPTIONS'
;
public const
CURL_PROTOCOL_HTTP = 'http',
CURL_PROTOCOL_HTTPS = 'https',
CURL_PROTOCOL_FTP = 'ftp',
CURL_PROTOCOL_FTPS = 'ftps',
CURL_PROTOCOL_SMTP = 'smtp',
CURL_PROTOCOL_NTP = 'ntp'
;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/** @var bool|null */
private $ssl_verify_host = null;
/** @var bool|null */
private $ssl_verify_peer = null;
/** @var bool */
private $use_cache = false;
/** @var array */
private $headers = [];
/** @var array */
private $customOptions = [];
/** @var string|null */
private $method = null;
/**@var string|null */
private $curlError;
/** @var array|null */
private $curlInfo;
/** @var int|null */
private $httpCode;
/** @var string|bool */
private $response;
/** @var string */
private $postFields = '';
/** @var null */
private $ch = null;
/** @var int */
private $timeout = 0;
/** @var string|null */
private $uri = null;
/** @var string|null */
private $cert = null;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @param string $uri
* @param array $queryArgs
* @param bool $doUseUrlEncode
* @param bool $useCache
*/
public function createRequest(string $uri, array $queryArgs = [], bool $doUseUrlEncode = false, bool $useCache = false): void
{
$this->clear();
$this->setUseCache($useCache);
if(!empty($queryArgs)) {
$query = parse_url($uri, PHP_URL_QUERY);
$firstArg = !$query;
foreach($queryArgs as $key => $value) {
if($doUseUrlEncode) {
$value = rawurlencode(utf8_encode($value));
}
if($firstArg) {
$uri .= sprintf('?%s=%s', $key, $value);
$firstArg = false;
} else {
$uri .= sprintf('&%s=%s', $key, $value);
}
}
}
if (!isset($this->ch) || $this->ch === null) {
$this->ch = $this->configureCurlClientBase($uri);
} else {
throw new SingleRuntimeException('Curl client is already running.');
}
}
/**
*
*/
public function execute(): void
{
$this->configureCurlClientRequest($this->ch);
$this->configureCurlClientResponse($this->ch);
unset($this->ch);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @param $curlClient
* @return mixed
*/
private function configureCurlClientRequest(&$curlClient)
{
if($this->isSslVerifyHost() !== null) {
curl_setopt($curlClient, CURLOPT_SSL_VERIFYHOST, $this->isSslVerifyHost() ? '2' : false);
}
if($this->isSslVerifyPeer() !== null) {
curl_setopt($curlClient, CURLOPT_SSL_VERIFYPEER, $this->isSslVerifyPeer());
}
if($this->getCert() !== null) {
curl_setopt($curlClient, CURLOPT_CAINFO, $this->getCert());
}
curl_setopt($curlClient, CURLOPT_HEADER, false);
if(!$this->isUseCache()) {
$this->addHeaders('Cache-Control: no-cache');
curl_setopt($curlClient, CURLOPT_FRESH_CONNECT, true);
}
curl_setopt($curlClient, CURLOPT_HTTPHEADER, $this->getHeaders());
if($this->getMethod() === self::CURL_METHOD_POST) {
curl_setopt($curlClient, CURLOPT_POST, true);
} else {
curl_setopt($curlClient, CURLOPT_CUSTOMREQUEST, $this->getMethod() ?? self::CURL_METHOD_GET);
}
if (!empty($this->getCustomOptions())) {
foreach ($this->getCustomOptions() as $option => $value) {
curl_setopt($curlClient, $option, $value);
}
}
curl_setopt($curlClient, CURLOPT_FRESH_CONNECT, $this->isUseCache());
curl_setopt($curlClient, CURLOPT_POSTFIELDS, $this->getPostFields());
curl_setopt($curlClient, CURLOPT_TIMEOUT, $this->getTimeout());
return $curlClient;
}
/**
* @param $curlClient
* @return mixed
*/
private function configureCurlClientResponse(&$curlClient)
{
$result = curl_exec($curlClient);
$this->setResponse($result);
if (curl_errno($curlClient)) {
$this->setCurlError(curl_error($curlClient));
}
$this->setHttpCode(curl_getinfo($curlClient, CURLINFO_HTTP_CODE));
$this->setCurlInfo(curl_getinfo($curlClient));
curl_close($curlClient);
return $curlClient;
}
/**
* @param string $uri
* @return false|resource
*/
private function configureCurlClientBase(string $uri)
{
$this->setUri($uri);
$curlClient = curl_init($uri);
curl_setopt($curlClient, CURLOPT_FAILONERROR, true);
curl_setopt($curlClient, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlClient, CURLOPT_RETURNTRANSFER, true);
return $curlClient;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @return bool
*/
public function isUseCache(): bool
{
return $this->use_cache;
}
/**
* @param bool $use_cache
*/
public function setUseCache(bool $use_cache): void
{
$this->use_cache = $use_cache;
}
/**
* @return string|null
*/
public function getCert(): ?string
{
return $this->cert;
}
/**
* @param string|null $cert
*/
public function setCert(?string $cert): void
{
$this->cert = $cert;
}
/**
* @return string|null
*/
public function getUri(): ?string
{
return $this->uri;
}
/**
* @param string|null $uri
*/
private function setUri(?string $uri): void
{
$this->uri = $uri;
}
/**
* @return int
*/
public function getTimeout(): int
{
return $this->timeout;
}
/**
* @param int $timeout
*/
public function setTimeout(int $timeout): void
{
$this->timeout = $timeout;
}
/**
* @return string
*/
public function getPostFields(): string
{
return $this->postFields;
}
/**
* @param string $postFields
*/
public function setPostFields(string $postFields): void
{
$this->postFields = $postFields;
}
/**
* @return bool|string
*/
public function getResponse()
{
return $this->response;
}
/**
* @param bool|string $response
*/
private function setResponse($response): void
{
$this->response = $response;
}
/**
* @return int|null
*/
public function getHttpCode(): ?int
{
return $this->httpCode;
}
/**
* @param int|null $httpCode
*/
private function setHttpCode(?int $httpCode): void
{
$this->httpCode = $httpCode;
}
/**
* @return array|null
*/
public function getCurlInfo(): ?array
{
return $this->curlInfo;
}
/**
* @param array|null $curlInfo
*/
private function setCurlInfo(?array $curlInfo): void
{
$this->curlInfo = $curlInfo;
}
/**
* @return string|null
*/
public function getCurlError(): ?string
{
return $this->curlError;
}
/**
* @param string|null $curlError
*/
private function setCurlError(?string $curlError): void
{
$this->curlError = $curlError;
}
/**
* @return string|null
*/
public function getMethod(): ?string
{
return $this->method;
}
/**
* @param string $method
*/
public function setMethod(string $method): void
{
$this->method = $method;
}
/**
* @return array
*/
public function getCustomOptions(): array
{
return $this->customOptions;
}
/**
* @param array $customOptions
*/
public function setCustomOptions(array $customOptions): void
{
$this->customOptions = $customOptions;
}
/**
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* @param array $headers
*/
public function setHeaders(array $headers): void
{
$this->headers = $headers;
}
/**
* @param string $header
*/
public function addHeaders(string $header): void
{
$this->headers[] = $header;
}
/**
* @return bool|null
*/
public function isSslVerifyHost(): ?bool
{
return $this->ssl_verify_host;
}
/**
* @param bool|null $ssl_verify_host
*/
public function setSslVerifyHost(?bool $ssl_verify_host): void
{
$this->ssl_verify_host = $ssl_verify_host;
}
/**
* @return bool|null
*/
public function isSslVerifyPeer(): ?bool
{
return $this->ssl_verify_peer;
}
/**
* @param bool|null $ssl_verify_peer
*/
public function setSslVerifyPeer(?bool $ssl_verify_peer): void
{
$this->ssl_verify_peer = $ssl_verify_peer;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*
*/
public function clear(): void
{
foreach($this as $key => $value) {
if($key === 'postFields') {
$this->$key = '';
continue;
}
if(is_array($value)) {
$this->$key = [];
} else if(is_int($value)) {
$this->$key = 0;
} else {
$this->$key = null;
}
}
}
/**
*
*/
public function debug(): void
{
bdump($this, __CLASS__);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Class SingleRuntimeException
* @package App
*/
class SingleRuntimeException extends RuntimeException {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.