1
0

ApiHandler.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace components;
  3. class ApiHandler
  4. {
  5. protected static $_instance = null;
  6. static function getInstance()
  7. {
  8. if (is_null(self::$_instance)) {
  9. self::$_instance = new self;
  10. }
  11. return self::$_instance;
  12. }
  13. /**
  14. * @param string $message
  15. */
  16. public function log($message)
  17. {
  18. if (false) {
  19. //Log::info($message);
  20. }
  21. }
  22. public function get($uri, $params = array())
  23. {
  24. return $this->_do_request($uri, $params);
  25. }
  26. public function post($uri, $params)
  27. {
  28. return $this->_do_request($uri, $params, true);
  29. }
  30. /**
  31. * 生成签名
  32. *
  33. * @return array
  34. */
  35. private function _generate_signature()
  36. {
  37. $timestamp = time();
  38. app()->configure('key');
  39. $private_key = config('key.apiAuthPrivateKey');
  40. $openssl_util = new OpensslAuth();
  41. $sig = $openssl_util->generate_signature($timestamp, $private_key);
  42. return [$timestamp, $sig];
  43. }
  44. /**
  45. * 订单流程唯一 key
  46. */
  47. private function _get_user_unique_key()
  48. {
  49. if(isset($_COOKIE["trade_user_unique_key"])) {
  50. $session_id = $_COOKIE["trade_user_unique_key"];
  51. } else {
  52. $session_id = md5(time());
  53. setcookie('trade_user_unique_key', $session_id, 0, '/');
  54. }
  55. return $session_id;
  56. }
  57. /**
  58. * 生成url
  59. *
  60. * @param $uri
  61. * @return string
  62. */
  63. private function _generate_url($uri)
  64. {
  65. $host = 'https://doc.33.cn';
  66. $url = rtrim($host, '/') . '/' . ltrim($uri, '/');
  67. //调试使用
  68. $this->log("call api: {$url}");
  69. return $url;
  70. }
  71. /**
  72. * 请求接口
  73. *
  74. * @param $uri
  75. * @param $params
  76. * @param bool $is_post
  77. * @return mixed
  78. * @throws ErrorException
  79. * @throws NotFoundException
  80. * @throws ParamException
  81. * @throws UserException
  82. * @throws UserLoglessException
  83. */
  84. private function _do_request($uri, $params, $is_post = false)
  85. {
  86. $url = $this->_generate_url($uri);
  87. var_dump('url:---'.$url);
  88. if (!$is_post) {
  89. if ($params) {
  90. $p_str = '';
  91. $comma = '';
  92. foreach ($params as $k => $v) {
  93. $p_str .= $comma . $k . '=' . $v;
  94. $comma = '&';
  95. }
  96. $url = $url . '?' . $p_str;
  97. }
  98. }
  99. $ch = curl_init();
  100. curl_setopt($ch, CURLOPT_URL, $url);
  101. curl_setopt($ch, CURLOPT_HEADER, 0);
  102. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  103. if ($is_post) {
  104. curl_setopt($ch, CURLOPT_POST, 1);
  105. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
  106. }
  107. $output = curl_exec($ch);
  108. return $output;
  109. }
  110. }