Created
June 5, 2017 22:12
-
-
Save alfredoem/9336fd97231726c0b72845edf9c9f1d8 to your computer and use it in GitHub Desktop.
HTTP Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class HttpRequest | |
{ | |
protected $host = ''; | |
protected $endpoint = ''; | |
public $url = ''; | |
public $errno = ''; | |
public $error = ''; | |
public $http_status = 0; | |
public $response; | |
public $responseRaw = ''; | |
public function __construct($host, $endpoint = '') | |
{ | |
$this->host = $host; | |
$this->endpoint = $endpoint; | |
} | |
public function setEndpoint($endpoint) | |
{ | |
$this->endpoint = $endpoint; | |
} | |
public function send($data) | |
{ | |
$payload = json_encode($data); | |
$this->url = $this->host . '/' . $this->endpoint; | |
$ch = curl_init($this->url); | |
$options = [ | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_POSTFIELDS => $payload, | |
CURLOPT_HTTPHEADER => [ | |
"Content-Type: application/json" | |
] | |
]; | |
curl_setopt_array($ch,$options); | |
$this->responseRaw = curl_exec($ch); | |
if (is_null($this->responseRaw) === false) { | |
$this->response = json_decode($this->responseRaw, true); | |
} | |
$this->errno = curl_errno($ch); | |
$this->error = curl_error($ch); | |
$this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return $this; | |
} | |
public function get($parameters = array()) | |
{ | |
$this->url = $this->host . '/' . $this->endpoint; | |
$ch = curl_init(); | |
curl_setopt_array($ch, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $this->url . '?' . http_build_query($parameters) | |
)); | |
$this->responseRaw = curl_exec($ch); | |
if (is_null($this->responseRaw) === false) { | |
$explode = explode('|', $this->responseRaw); | |
$this->response = [ | |
'code' => $explode[0], | |
'codeText' => $this->getTextCode($explode[0]), | |
'description' => $explode[1] | |
]; | |
} | |
$this->errno = curl_errno($ch); | |
$this->error = curl_error($ch); | |
$this->http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
return $this; | |
} | |
public function getOk($parameters = array()) | |
{ | |
$this->http_status = 200; | |
$this->response = [ | |
'code' => 0, | |
'codeText' => $this->getTextCode(0), | |
'description' => 'Success' | |
]; | |
return $this; | |
} | |
public function getFail($code = -1) | |
{ | |
$this->http_status = 200; | |
$this->response = [ | |
'code' => $code, | |
'codeText' => $this->getTextCode($code), | |
'description' => 'Lorem ipsum dolor sit amet' | |
]; | |
return $this; | |
} | |
private function getTextCode($code) | |
{ | |
$textCode = 'TRY_AGAIN'; | |
if ($code == 0) { | |
$textCode = 'SUCCESS'; | |
} elseif ($code == -14) { | |
$textCode = 'PIN_EXPIRED'; | |
} elseif ($code == -15) { | |
$textCode = 'PIN_INVALID'; | |
} | |
return $textCode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment