123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- /**
- * Created by PhpStorm.
- * User: tatu
- * Date: 6/22/20
- * Time: 9:58 PM
- */
- namespace App\Lib;
- use GuzzleHttp\Client as Guzzle;
- use Psr\Http\Message\StreamInterface;
- use GuzzleHttp\Exception\ClientException;
- class Backend
- {
- protected $url;
- public function __construct()
- {
- $this->url = config('stag.backendUrl');
- }
- public function post(string $url, array $data)
- {
- return $this->sendRequest($url, 'POST', ['form_params' => $data]);
- }
- public function get(string $url, array $data = [])
- {
- return $this->sendRequest($url, 'GET', $data);
- }
- /**
- * @param string $url
- * @param string $method HTTP request method eg POST, GET.
- * @param array $options
- * @return StreamInterface
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function sendRequest(string $url, string $method, array $options = []) : StreamInterface
- {
- $url = sprintf('%s/%s', rtrim($this->url, '/'), trim($url, '/'));
- $response = null;
- $client = new Guzzle();
- try {
- $response = $client->request($method, $url, $options);
- }
- catch (ClientException $clientException) {
- $response = $clientException->getResponse();
- }
- return $response->getBody();
- }
- }
|