PayClient.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Libs;
  3. use GuzzleHttp\Client;
  4. /**
  5. * api
  6. * 请求client
  7. */
  8. class PayClient
  9. {
  10. static private $instance;
  11. static private $client;
  12. function __construct($base_uri,$timeout)
  13. {
  14. self::$client = new Client([
  15. 'base_uri' => $base_uri,
  16. 'timeout' => $timeout,
  17. ]);
  18. }
  19. static function instance()
  20. {
  21. if(!self::$instance)
  22. {
  23. self::$instance = new self();
  24. }
  25. return self::$instance;
  26. }
  27. static function get($url,$params=[])
  28. {
  29. try {
  30. if($params)
  31. $response = self::$client->request('get',$url.'?'.http_build_query($params));
  32. else
  33. $response = self::$client->request('get',$url);
  34. $res = self::format($response);
  35. return $res;
  36. }catch(\Exception $e)
  37. {
  38. \Log::info($e);
  39. return false;
  40. }
  41. }
  42. static function post($url,$params=[])
  43. {
  44. try {
  45. $response = self::$client->request('post',$url,['form_params'=>$params]);
  46. return self::format($response);
  47. }catch(\Exception $e)
  48. {
  49. \Log::info($e);
  50. return false;
  51. }
  52. }
  53. static function format($response)
  54. {
  55. try {
  56. return json_decode($response->getBody()->getContents(),1);
  57. }catch(\Exception $e)
  58. {
  59. return false;
  60. }
  61. }
  62. }