Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace GeoMonitoring\Base\Adapters;
- use GuzzleHttp\Client;
- class CurlAdapter
- {
- private $headers = [];
- private $params = [];
- private $method = null;
- private $url = null;
- private $client;
- public function __construct()
- {
- $this->client = new Client();
- }
- public function get(string $url, array $params = [])
- {
- $this->method = 'GET';
- $this->url = $url;
- array_push($this->params, $params);
- return $this->execute();
- }
- public function post(string $url, array $params = [])
- {
- $this->method = 'POST';
- $this->url = $url;
- $this->params = $params;
- return $this->execute();
- }
- public function setHeader(string $name, string $value)
- {
- $this->headers[$name] = $value;
- return $this;
- }
- public function acceptJson()
- {
- $this->headers['Accept'] = 'application/json';
- return $this;
- }
- /**
- * @return mixed|\Psr\Http\Message\ResponseInterface
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- private function execute()
- {
- $requestData = [
- 'headers' => $this->headers,
- 'connect_timeout' => 10,
- ];
- switch ($this->method)
- {
- case 'GET':
- $requestData['query'] = $this->params;
- break;
- case 'POST':
- $requestData['form_params'] = $this->params;
- break;
- }
- return $this->client->request($this->method, $this->url, $requestData);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement