Created
July 26, 2018 14:29
-
-
Save decodedmrq/de2b4816bfde93d042746cbe29c98715 to your computer and use it in GitHub Desktop.
Consuming Api with GuzzleHTTP
This file contains hidden or 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 | |
return [ | |
"default" => [ | |
/** | |
* Base URL of Rest service | |
*/ | |
'base_uri' => 'http://localhost/api/', | |
/** | |
* Float describing the timeout of the request in seconds. | |
*/ | |
'timeout' => 5.0, | |
/** | |
* Describes the redirect behavior of a request | |
* Set to false to disable redirects | |
*/ | |
"allow_redirects" => [ | |
'max' => 5, | |
'strict' => false, | |
'referer' => false, | |
'protocols' => ['http', 'https'], | |
'track_redirects' => false | |
], | |
/** | |
* Set to false to disable throwing exceptions on an HTTP protocol errors | |
*/ | |
"http_errors" => false, | |
/** | |
* Specify whether or not Content-Encoding responses (gzip, deflate, etc.) are automatically decoded. | |
*/ | |
"decode_content" => true, | |
/** | |
* Describes the SSL certificate verification behavior of a request. | |
* Set to true to enable SSL certificate verification and use the default CA bundle provided by operating system. | |
* Set to false to disable certificate verification (this is insecure!). | |
* Set to a string to provide the path to a CA bundle to enable verification using a custom certificate. | |
*/ | |
"verify" => false, | |
/** | |
* Specifies whether or not cookies are used in a request or what cookie jar to use or what cookies to send | |
*/ | |
"cookies" => false, | |
/** | |
* Associative array of headers to add to the request. Each key is the name of a header, | |
* and each value is a string or array of strings representing the header field values. | |
*/ | |
"headers" => [ | |
"User-Agent" => "GuzzleHttp/6.2.1 curl/7.49.1 PHP/5.6.19", | |
'Accept' => 'application/json', | |
] | |
] | |
/* | |
* For more guzzle request options | |
* Browse Guzzle docs : http://docs.guzzlephp.org/en/latest/request-options.html | |
* | |
*/ | |
]; |
This file contains hidden or 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 | |
namespace Basemkhirat\API; | |
use Illuminate\Support\Facades\Config; | |
use GuzzleHttp\Client; | |
/** | |
* Class API | |
* @package Basemkhirat\API | |
*/ | |
class API | |
{ | |
/** | |
* @var Client | |
*/ | |
protected $client; | |
/** | |
* @var Response | |
*/ | |
protected $response; | |
/** | |
* @var string | |
*/ | |
protected $driver = "default"; | |
/** | |
* API constructor. | |
* @param array $config | |
*/ | |
function __construct($config = []) | |
{ | |
$this->client = new Client($config); | |
} | |
/** | |
* @param string $driver | |
* @return $this | |
*/ | |
function driver($driver = "default") | |
{ | |
$this->driver = $driver; | |
$this->client = new Client(Config::get("api.$driver", [])); | |
return $this; | |
} | |
/** | |
* @return GuzzleClient object to make raw queries | |
*/ | |
function guzzle() | |
{ | |
return $this->client; | |
} | |
/** | |
* @param $method | |
* @param string $uri | |
* @param array $options | |
* @return $this | |
*/ | |
public function request($method, $uri = '', array $options = []) | |
{ | |
$this->response = $this->client->request($method, $uri, $options); | |
return $this; | |
} | |
/** | |
* @param bool $path | |
* @param array $config | |
* @return mixed|\Psr\Http\Message\ResponseInterface | |
*/ | |
public function get($path = false, $config = []) | |
{ | |
return $this->request("GET", $path, $config); | |
} | |
/** | |
* @param bool $path | |
* @param array $config | |
* @return mixed|\Psr\Http\Message\ResponseInterface | |
*/ | |
public function post($path = false, $config = []) | |
{ | |
return $this->request("POST", $path, $config); | |
} | |
/** | |
* @param bool $path | |
* @param array $config | |
* @return mixed|\Psr\Http\Message\ResponseInterface | |
*/ | |
public function put($path = false, $config = []) | |
{ | |
return $this->request("PUT", $path, $config); | |
} | |
/** | |
* @param bool $path | |
* @param array $config | |
* @return mixed|\Psr\Http\Message\ResponseInterface | |
*/ | |
public function delete($path = false, $config = []) | |
{ | |
return $this->request("DELETE", $path, $config); | |
} | |
/** | |
* Get body content array | |
* @return mixed | |
*/ | |
public function toArray() | |
{ | |
$accept = config("api.$this->driver.headers.Accept", "application/json"); | |
$contents = $this->response->getBody()->getContents(); | |
if ($accept == "application/xml") { | |
return simplexml_load_string($contents); | |
} else { | |
return json_decode($contents); | |
} | |
} | |
/** | |
* Get body content array | |
* @return mixed | |
*/ | |
public function getContent() | |
{ | |
return $this->response->getBody()->getContents(); | |
} | |
/** | |
* @param $name | |
* @param $arguments | |
* @return mixed | |
*/ | |
function __call($name, $arguments) | |
{ | |
if (method_exists($this, $name)) { | |
return call_user_func_array([$this, $name], $arguments); | |
} elseif (method_exists($this->response, $name)) { | |
return call_user_func_array([$this->response, $name], $arguments); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment