Skip to content

Instantly share code, notes, and snippets.

@ugursogukpinar
Created May 20, 2015 11:37
Show Gist options
  • Save ugursogukpinar/b181dd490f7233d761e6 to your computer and use it in GitHub Desktop.
Save ugursogukpinar/b181dd490f7233d761e6 to your computer and use it in GitHub Desktop.
Php curl HTTP class
class Http {
private $url;
private $postString;
private $httpResponse;
private $ch;
private $headers;
private $errNo;
public function __construct($url , $access_token=null)
{
$this->url = $url;
$this->errNo = null;
$this->ch = curl_init($this->url);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($this->ch, CURLOPT_HEADER, false);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->ch,CURLOPT_TIMEOUT,1000);
if(!empty($access_token))
{
$this->headers = array(
sprintf('authorization: Bearer %s', $access_token)
);
} else
{
$this->headers = array();
}
}
public function __destruct()
{
curl_close($this->ch);
}
public function setPostData($paramStr)
{
$this->postString = $paramStr;
curl_setopt($this->ch, CURLOPT_POST, true);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->postString);
}
public function send()
{
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
$this->httpResponse = curl_exec($this->ch);
$this->errNo = curl_errno($this->ch);
}
public function getHttpResponse()
{
$this->errNo = null;
return $this->httpResponse;
}
public function isSuccess()
{
return !$this->errNo;
}
public function getErrorMessage()
{
if($this->errNo)
{
return curl_strerror($this->errNo);
}
return null;
}
public function setMethod($method)
{
$method = strtoupper($method);
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
}
public function setHeader($header=array())
{
$this->headers = array_merge($header,$this->headers);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment