Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. namespace GeoMonitoring\Base\Adapters;
  5.  
  6. use GuzzleHttp\Client;
  7.  
  8. class CurlAdapter
  9. {
  10.     private $headers = [];
  11.     private $params = [];
  12.     private $method = null;
  13.     private $url = null;
  14.     private $client;
  15.  
  16.     public function __construct()
  17.     {
  18.         $this->client = new Client();
  19.     }
  20.  
  21.     public function get(string $url, array $params = [])
  22.     {
  23.         $this->method = 'GET';
  24.         $this->url = $url;
  25.         array_push($this->params, $params);
  26.         return $this->execute();
  27.     }
  28.  
  29.     public function post(string $url, array $params = [])
  30.     {
  31.         $this->method = 'POST';
  32.         $this->url = $url;
  33.         $this->params = $params;
  34.  
  35.         return $this->execute();
  36.     }
  37.  
  38.     public function setHeader(string $name, string $value)
  39.     {
  40.         $this->headers[$name] = $value;
  41.         return $this;
  42.     }
  43.  
  44.     public function acceptJson()
  45.     {
  46.         $this->headers['Accept'] = 'application/json';
  47.  
  48.         return $this;
  49.     }
  50.  
  51.     /**
  52.      * @return mixed|\Psr\Http\Message\ResponseInterface
  53.      * @throws \GuzzleHttp\Exception\GuzzleException
  54.      */
  55.     private function execute()
  56.     {
  57.  
  58.         $requestData = [
  59.             'headers' => $this->headers,
  60.             'connect_timeout' => 10,
  61.  
  62.         ];
  63.         switch ($this->method)
  64.         {
  65.             case 'GET':
  66.                 $requestData['query'] = $this->params;
  67.                 break;
  68.  
  69.             case 'POST':
  70.                 $requestData['form_params'] = $this->params;
  71.                 break;
  72.         }
  73.  
  74.         return $this->client->request($this->method, $this->url, $requestData);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement