<?php
 use TCMS\TException;
 class HttpRequest {
 private $address;
 private $connectTimeout = 10;
 private $receiveTimeout = 15;
 private $cookiesEnabled = FALSE;
 private $cookiePath;
 private $ssl = FALSE;
 private $requestType;
 private $postData;
 private $userpwd;
 private $responseBody;
 private $responseHeader;
 private $httpCode;
 private $error;
 private $authHeader = '';
 private $contentType = '';
 public function __construct($address, $method = 'GET') {
 if (!isset($address)) {
 throw new TException("Error: Vui lòng truyền address.");
 }
 $this->address = $address;
 $this->setMethod($method);
 }
 public function setAddress($address) {
 $this->address = $address;
 }
 public function setBasicAuthCredentials($username, $password) {
 $this->userpwd = $username . ':' . $password;
 }
 public function setAuthHeader($token) {
 $this->authHeader = $token;
 }
 public function setContentType($contentType) {
 $this->contentType = $contentType;
 }
 public function enableCookies($cookie_path) {
 $this->cookiesEnabled = TRUE;
 $this->cookiePath = $cookie_path;
 }
 public function disableCookies() {
 $this->cookiesEnabled = FALSE;
 $this->cookiePath = '';
 }
 public function enableSSL() {
 $this->ssl = TRUE;
 }
 public function disableSSL() {
 $this->ssl = FALSE;
 }
 public function setReceiveTimeout($receiveTimeout = 15) {
 $this->receiveTimeout = $receiveTimeout;
 }
 public function setConnectTimeout($connectTimeout = 10) {
 $this->connectTimeout = $connectTimeout;
 }
 public function setMethod($type) {
 $this->requestType = $type;
 }
 public function setPostData($data = array()) {
 $this->postData = $data;
 }
 public function getResponse() {
 return $this->responseBody;
 }
 public function getHeader() {
 return $this->responseHeader;
 }
 public function getHttpCode() {
 return $this->httpCode;
 }
 public function getError() {
 return $this->error;
 }
 public function execute() {
 $ch = curl_init();
 if (isset($this->userpwd)) {
 curl_setopt($ch, CURLOPT_USERPWD, $this->userpwd);
 }
 if ($this->cookiesEnabled) {
 curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiePath);
 curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiePath);
 }
 if (isset($this->requestType)) {
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->requestType);
 if ($this->requestType == 'POST' && isset($this->postData)) {
 curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->postData));
 }
 }
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
 curl_setopt($ch, CURLOPT_URL, $this->address);
 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
 curl_setopt($ch, CURLOPT_TIMEOUT, $this->receiveTimeout);
 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
 curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
 if($this->authHeader || $this->contentType) {
 $h = [];
 if($this->authHeader) {
 $h[] = 'Authorization: ' . $this->authHeader;
 }
 if($this->contentType) {
 $h[] = 'Content-Type: ' . $this->contentType;
 }
 curl_setopt($ch, CURLOPT_HTTPHEADER, $h);
 }
 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->ssl);
 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; PHP Request library)');
 curl_setopt($ch, CURLOPT_HEADER, TRUE);
 $response = curl_exec($ch);
 $error = curl_error($ch);
 $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
 $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
 $time = curl_getinfo($ch, CURLINFO_TOTAL_TIME);
 curl_close($ch);
 $this->responseHeader = substr($response, 0, $header_size);
 $this->responseBody = substr($response, $header_size);
 $this->error = $error;
 $this->httpCode = $http_code;
 if ($this->httpCode == 200 || $this->httpCode == 304) {
 return $this->responseBody;
 }
 return FALSE;
 }
 public function execJson() {
 $res = $this->execute();
 if($res) {
 return json_decode($res);
 }
 return false;
 }
 }
 