Skip to content

Instantly share code, notes, and snippets.

@marvell
Forked from obukhov/CurlRequest.php
Created January 12, 2014 17:18
Show Gist options
  • Save marvell/8387622 to your computer and use it in GitHub Desktop.
Save marvell/8387622 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by JetBrains PhpStorm.
* User: aobukhov
* Date: 1/15/13
* Time: 7:40 PM
*/
class CurlRequest implements CurlInterface
{
private $curlResource = null;
/**
* Конструктор
*
* @param $url адрес запроса
*/
public function __construct($url)
{
$this->curlResource = curl_init($url);
}
/**
* Установить опцию запроса
*
* @param $option имя опции
* @param $value значение опции
* @return bool
*/
public function setOption($option, $value)
{
$this->checkState();
return curl_setopt($this->curlResource, $option, $value);
}
/**
* Выполнить запрос
*
* @return string ответ сервера
*/
public function exec()
{
$this->checkState();
return curl_exec($this->curlResource);
}
/**
* Получить номер ошибки
* @return int|null
*/
public function getErrorCode()
{
$this->checkState();
return curl_errno($this->curlResource);
}
/**
* Получить сообщение об ошибке
* @return string|null
*/
public function getErrorMessage()
{
$this->checkState();
return curl_error($this->curlResource);
}
/**
* Закрыть сессию
* @return void
*/
public function curlClose()
{
$this->checkState();
$r = curl_close($this->curlResource);
$this->curlResource = null;
return $r;
}
/**
* Получить информацию о запросе
* @param null $option
* @return mixed
*/
public function getInfo($option = null)
{
$this->checkState();
return curl_getinfo($this->curlResource, $option);
}
public function __destruct()
{
if (!is_null($this->curlResource)) {
$this->curlClose();
}
}
private function checkState()
{
if ($this->curlResource) {
throw new Exception('Session is closed');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment