*/
abstract class Curl
{
private $curl;
private $options = [
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true
];
protected $curlParser;
public function setCurlParser($curlParser)
{
$this->curlParser = $curlParser;
return $this;
}
public function setOptions($options, $value)
{
$this->options[$options] = $value;
return $this;
}
protected function open()
{
$this->curl = curl_init();
return $this;
}
protected function close()
{
curl_close($this->curl);
return $this;
}
protected function unsetOptions($options)
{
if (is_array($options))
{
foreach ($options as $option)
{
if (isset($this->options[$option]))
{
unset($this->options[$option]);
}
}
}
else
{
unset($this->options[$options]);
}
return $this;
}
/**
* @return Response
* @throws \ModulesGarden\Servers\ProxmoxVps\Core\HandlerError\Exceptions\Exception
*/
protected function send()
{
$this->includeOptions();
if (($head = $this->execute()) === false)
{
throw new Exception(ErrorCodesLib::CORE_CURL_000001, ['lastCurlError' => $this->getLastErrorWithCurl()]);
}
if ($errno = $this->getLastErrorNumber())
{
throw new Exception(ErrorCodesLib::CORE_CURL_000002, ['curlError' => $this->getLastError($errno)]);
}
list($header, $body) = $this->curlParser->rebuild($head, $this->getHeaderSize());
return DependencyInjection::create(Response::class)
->setRequest($this->getHeaderOut())
->setHeader($header)
->setCode($this->getHttpCode())
->setBody($body);
}
private function execute()
{
return curl_exec($this->curl);
}
private function getLastErrorNumber()
{
return curl_errno($this->curl);
}
/**
* (PHP 5 >= 5.5.0, PHP 7)
* Return string describing the given error code
* @link http://php.net/manual/en/function.curl-strerror.php
* @param int $errornum
* One of the cURL error codes constants. *
* @return string error description or NULL for invalid error code. */ private function getLastError($errmo) { return curl_strerror($errmo); } /** * (PHP 4 >= 4.0.3, PHP 5, PHP 7)