<?php
namespace App\Libs;

use GuzzleHttp\Client;

/**
 * api
 * 请求client
 */
class PayClient
{
	static private $instance;
	static private $client;

	function __construct($base_uri,$timeout)
	{
		self::$client = new Client([
			    'base_uri' => $base_uri,
			    'timeout'  => $timeout,
		    ]);
	}

    static function instance()
    {
    	if(!self::$instance)
    	{
    		self::$instance = new self();
    	}
    	return self::$instance;
    }

    static function get($url,$params=[])
    {
    	try {
            if($params)
    		    $response = self::$client->request('get',$url.'?'.http_build_query($params));
            else
                $response = self::$client->request('get',$url);

    		$res =  self::format($response);

            return $res;

    	}catch(\Exception $e)
    	{
    	    \Log::info($e);
    		return false;
    	}
    	
    }

    static function post($url,$params=[])
    {

    	try {

	    	$response = self::$client->request('post',$url,['form_params'=>$params]);
	    	return self::format($response);

	    }catch(\Exception $e)
    	{
    	    \Log::info($e);
    		return false;
    	}
    }

    static function format($response)
    {
    	try {
    		return json_decode($response->getBody()->getContents(),1);
    	}catch(\Exception $e)
    	{
    		return false;	
    	}
    }

}