Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active November 7, 2019 07:49
Show Gist options
  • Save afiqiqmal/777fc6383ffce11113dc379094ee18b4 to your computer and use it in GitHub Desktop.
Save afiqiqmal/777fc6383ffce11113dc379094ee18b4 to your computer and use it in GitHub Desktop.
Api Request + HTTP Guzzle PHP

Api Request + HTTP Guzzle using PHP

<?php
namespace App\Http\Services;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
define('METHOD_POST', 'POST');
define('METHOD_GET', 'GET');
define('METHOD_PATCH', 'PATCH');
define('METHOD_DELETE', 'DELETE');
class ApiRequest
{
protected $baseUrl = null;
protected $requestBody = [];
protected $param = [];
protected $method = 'GET';
protected $requestUrl = null;
protected $header = null;
protected $isRaw = false;
protected $verifySSL = true;
protected $isJsonRequest = false;
protected $initOption = [];
public function baseUrl($url)
{
$this->baseUrl = $url;
return $this;
}
public function setParam($option = [])
{
$this->param = $option;
return $this;
}
public function setRequestBody($param = [])
{
$this->requestBody = $param;
return $this;
}
public function jsonRequest()
{
$this->isJsonRequest = true;
return $this;
}
public function getMethod()
{
$this->method = 'GET';
return $this;
}
public function postMethod()
{
$this->method = 'POST';
return $this;
}
public function patchMethod()
{
$this->method = 'PATCH';
return $this;
}
public function deleteMethod()
{
$this->method = 'DELETE';
return $this;
}
public function setMethod($method = 'GET')
{
$this->method = $method;
return $this;
}
public function setHeader($header = null)
{
$this->header = $header;
return $this;
}
public function requestUrl($requestUrl)
{
$this->requestUrl = $requestUrl;
return $this;
}
public function getRaw()
{
$this->isRaw = true;
return $this;
}
public function ignoreSSL()
{
$this->verifySSL = false;
return $this;
}
public function fetch($requestUrl = null, $params = [], $method = null, $header = null)
{
if ($requestUrl) {
$this->requestUrl = $requestUrl;
}
if (count($params) > 0) {
$this->requestBody = $params;
}
if (count($this->param) > 0 && $this->requestBody == null) {
$this->requestBody = $this->param;
}
if ($method) {
$this->method = $method;
}
if ($header) {
$this->header = $header;
}
if (! $this->baseUrl) {
throw new \RuntimeException('Base URL need to be set!!');
}
if (substr($this->baseUrl, -1) != '/') {
$this->baseUrl = $this->baseUrl.'/';
}
if ($this->requestUrl && substr($this->requestUrl, -1) == '/') {
$this->requestUrl = ltrim($this->requestUrl, '/');
}
if (! $this->requestUrl) {
$this->baseUrl = rtrim($this->baseUrl, '/');
}
$url = $this->baseUrl.$this->requestUrl;
try {
$client = new Client();
switch ($this->method) {
case METHOD_GET:
$param = [
'query' => $this->requestBody,
'headers' => $this->header,
];
break;
case METHOD_POST:
case METHOD_PATCH:
case METHOD_DELETE:
if ($this->isJsonRequest) {
$param = [
'json' => $this->requestBody,
'headers' => $this->header,
];
} else {
$param = [
'form_params' => $this->requestBody,
'headers' => $this->header,
];
}
break;
default:
$param = null;
break;
}
if (! isset($param['headers']) && $param['headers'] == null) {
unset($param['headers']);
}
if (! $this->verifySSL) {
$param['verify'] = false;
}
$param = array_merge($param, $this->param);
$response = $client->request($this->method, $url, $param);
return [
'error' => false,
'body' => $this->isRaw ? $response->getBody()->getContents() : json_decode($response->getBody(), true),
'header' => $response->getHeaders(),
'status_code' => $response->getStatusCode(),
];
} catch (\Exception $ex) {
return [
'error' => true,
'message' => 'Server is currently unavailable',
'reference' => $ex->getMessage(),
];
} catch (GuzzleException $ex) {
return [
'error' => true,
'message' => 'Server is currently unavailable',
'reference' => $ex->getMessage(),
];
}
}
}
<?php
return api_request()
->baseUrl("https://example.com")
->setMethod($method) // ->getMethod() ->postMethod() ->patchMethod() ->deleteMethod()
->setRequestBody($params)
->setHeader($header)
->setParam([
'verify' => false
])
->requestUrl("getprofile.php")
->fetch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment