Http.php 1.7 KB
<?php


namespace App\Helper;


use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

/**
 * Class Response
 * @package App\Helper
 */
class Http
{
    const METHOD_POST = 'POST';
    const METHOD_GET = 'GET';

    /**
     * @param $url
     * @param array $params
     * @return array|mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function post($uri, $params = [])
    {
        $url = self::formatUrl($uri);

        try {
            $response = (new Client())->request(self::METHOD_POST, $url, [
                'json'=>$params
            ]);

            $data = json_decode($response->getBody()->getContents(), true);

            Log::channel('api')->info(__METHOD__, ['url'=>$url, 'params'=>$params, 'data'=>$data]);

            return $data;

        } catch (\Exception $e) {
            Log::channel('api')->info(__METHOD__, ['msg'=>$e->getMessage()]);
            return [];
        }
    }

    /**
     * @param $url
     * @return array|mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function get($uri)
    {
        $url = self::formatUrl($uri);

        try {
            $response = (new Client())->request(self::METHOD_POST, $url);

            $data = json_decode($response->getBody()->getContents(), true);

            Log::channel('api')->info(__METHOD__, ['url'=>$url, 'data'=>$data]);

            return $data;

        } catch (\Exception $e) {
            Log::channel('api')->info(__METHOD__, ['msg'=>$e->getMessage()]);
            return [];
        }
    }

    /**
     * @param $uri
     * @return string
     */
    public static function formatUrl($uri)
    {
        return env('musician_url') . $uri;
    }




}