Advertisement
robertl7

HttpClient

Sep 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.20 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Utils;
  4.  
  5. use GuzzleHttp\Client;
  6. use Psr\Http\Message\ResponseInterface;
  7.  
  8. /**
  9.  * Class HttpClient
  10.  *
  11.  * @package App\Utils
  12.  */
  13. class HttpClient
  14. {
  15.     const CONNECTION_TIMEOUT = 30;
  16.  
  17.     /**
  18.      * @var array|false|string
  19.      */
  20.     private $apiSecret;
  21.  
  22.     /**
  23.      * @var Client
  24.      */
  25.     protected $client;
  26.  
  27.     /**
  28.      * @var ResponseInterface
  29.      */
  30.     protected $lastResponse;
  31.  
  32.     /**
  33.      * HttpClient constructor.
  34.      * @param Client $client
  35.      */
  36.     public function __construct(Client $client)
  37.     {
  38.         $this->apiSecret = getenv('API_SECRET');
  39.         $this->client = $client;
  40.     }
  41.  
  42.     /**
  43.      * @param string $uri
  44.      * @param array $data
  45.      *
  46.      * @return mixed|\Psr\Http\Message\ResponseInterface
  47.      * @throws \GuzzleHttp\Exception\GuzzleException
  48.      */
  49.     public function makeGetRequest(string $uri, array $data = [], $hostHeader = false, $debug = false)
  50.     {
  51.         $attributes = $this->getBasicAttributes($data, $debug, $hostHeader);
  52.         $attributes['query'] = $data;
  53.  
  54.         try {
  55.             $lastResponse = $this->client->request('GET', $uri, $attributes);
  56.  
  57.             return $this->getLastResponseContent($lastResponse);
  58.         } catch (\Throwable $e) {
  59.             return false;
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * @param string $uri
  65.      * @param array $data
  66.      *
  67.      * @return mixed|\Psr\Http\Message\ResponseInterface
  68.      * @throws \GuzzleHttp\Exception\GuzzleException
  69.      */
  70.     public function makePostRequest(string $uri, array $data = [], $hostHeader = false, $debug = false)
  71.     {
  72.         $attributes = $this->getBasicAttributes($data, $debug, $hostHeader);
  73.         $attributes['form_params'] = $data;
  74.  
  75.         try {
  76.             $lastResponse = $this->client->request('POST', $uri, $attributes);
  77.  
  78.             return $this->getLastResponseContent($lastResponse);
  79.         } catch (\Throwable $e) {
  80.             return false;
  81.         }
  82.     }
  83.  
  84.     /**
  85.      * @param $lastResponse
  86.      * @param bool $decodeJson
  87.      * @return mixed|null
  88.      */
  89.     protected function getLastResponseContent($lastResponse, bool $decodeJson = true)
  90.     {
  91.         $responseContent = $lastResponse ? $lastResponse->getBody()->getContents() : null;
  92.  
  93.         return ($decodeJson && $responseContent) ? json_decode($responseContent) : $responseContent;
  94.     }
  95.  
  96.     /**
  97.      * @param array $content
  98.      *
  99.      * @return string
  100.      */
  101.     protected function generateSign(array $content)
  102.     {
  103.         $serializedContent = serialize($content);
  104.  
  105.         return hash_hmac('sha512', $serializedContent, $this->apiSecret);
  106.     }
  107.  
  108.     /**
  109.      * @param array $data
  110.      * @param bool $debug
  111.      * @param bool $hostHeader
  112.      * @return array
  113.      */
  114.     private function getBasicAttributes(array $data, bool $debug, bool $hostHeader): array
  115.     {
  116.         $attributes = [
  117.             'headers' => [
  118.                 'sign' => $this->generateSign($data)
  119.             ],
  120.             'connect_timeout' => self::CONNECTION_TIMEOUT,
  121.             'debug' => $debug
  122.         ];
  123.  
  124.         if ($hostHeader) {
  125.             $attributes['headers']['host'] = $hostHeader;
  126.         }
  127.  
  128.         return $attributes;
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement