SignClient.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace extend\api;
  3. class SignClient
  4. {
  5. private $version = '1.0';
  6. private $request_url;
  7. // private static $request_url = 'http://demo.niucloud.com/?s=/api/';
  8. private $app_key;
  9. private $app_secret;
  10. private $format = 'json';
  11. private $sign_method = 'md5';
  12. public function __construct($app_key, $app_secret)
  13. {
  14. if ('' == $app_key || '' == $app_secret) throw new \Exception('app_key 和 app_secret 不能为空');
  15. $this->app_key = $app_key;
  16. $this->app_secret = $app_secret;
  17. // $this->request_url = DOMAIN . '/?s=/api/';
  18. $this->request_url = API_URL . '/?s=/api/';
  19. }
  20. public function get($method, $api_version, $params = array())
  21. {
  22. return $this->parse_response(
  23. HttpClient::get($this->url($method, $api_version), $this->build_request_params($method, $params))
  24. );
  25. }
  26. public function post($method, $params = array(), $files = array(), $api_version = '1.0')
  27. {
  28. $this->version = $api_version;
  29. return $this->parse_response(
  30. HttpClient::post($this->url($method, $api_version), $this->build_request_params($method, $params), $files)
  31. );
  32. }
  33. public function url($method, $api_version = '1.0')
  34. {
  35. $method = 'index/index/method/' . $method . '/version/' . $api_version;
  36. $url = $this->request_url . $method;
  37. return $url;
  38. }
  39. public function set_format($format)
  40. {
  41. if (!in_array($format, ApiProtocol::allowed_format()))
  42. throw new \Exception('设置的数据格式错误');
  43. $this->format = $format;
  44. return $this;
  45. }
  46. public function set_sign_method($method)
  47. {
  48. if (!in_array($method, ApiProtocol::allowed_sign_methods()))
  49. throw new \Exception('设置的签名方法错误');
  50. $this->sign_method = $method;
  51. return $this;
  52. }
  53. private function parse_response($response_data)
  54. {
  55. $data = json_decode($response_data, true);
  56. if (null === $data) throw new \Exception('response invalid, data: ' . $response_data);
  57. return $data;
  58. }
  59. private function build_request_params($method, $api_params)
  60. {
  61. if (!is_array($api_params)) $api_params = array();
  62. if ($this->app_key) {
  63. }
  64. $pairs = $this->get_common_params($method);
  65. foreach ($api_params as $k => $v) {
  66. if (isset($pairs[ $k ])) throw new \Exception('参数名冲突');
  67. $pairs[ $k ] = $v;
  68. }
  69. $pairs[ ApiProtocol::SIGN_KEY ] = ApiProtocol::sign($this->app_secret, $pairs, $this->sign_method);
  70. return $pairs;
  71. }
  72. private function get_common_params($method)
  73. {
  74. $params = array();
  75. $params[ ApiProtocol::APP_ID_KEY ] = $this->app_key;
  76. $params[ ApiProtocol::METHOD_KEY ] = $method;
  77. $params[ ApiProtocol::TIMESTAMP_KEY ] = date('Y-m-d H:i:s');
  78. $params[ ApiProtocol::FORMAT_KEY ] = $this->format;
  79. $params[ ApiProtocol::SIGN_METHOD_KEY ] = $this->sign_method;
  80. $params[ ApiProtocol::VERSION_KEY ] = $this->version;
  81. return $params;
  82. }
  83. }