YZSignClient.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. require_once __DIR__ . '/YZApiProtocol.php';
  3. require_once __DIR__ . '/YZHttpClient.php';
  4. class YZSignClient
  5. {
  6. const VERSION = '1.0';
  7. private static $signUrl = 'https://open.youzan.com/api/entry/';
  8. private $appId;
  9. private $appSecret;
  10. private $format = 'json';
  11. private $signMethod = 'md5';
  12. public function __construct($appId, $appSecret)
  13. {
  14. if ('' == $appId || '' == $appSecret) throw new Exception('appId 和 appSecret 不能为空');
  15. $this->appId = $appId;
  16. $this->appSecret = $appSecret;
  17. }
  18. public function get($method, $methodVersion, $params = array())
  19. {
  20. return $this->parseResponse(
  21. YZHttpClient::get($this->url($method, $methodVersion), $this->buildRequestParams($method, $params))
  22. );
  23. }
  24. public function post($method, $methodVersion, $params = array(), $files = array())
  25. {
  26. return $this->parseResponse(
  27. YZHttpClient::post($this->url($method, $methodVersion), $this->buildRequestParams($method, $params), $files)
  28. );
  29. }
  30. public function url($method, $methodVersion)
  31. {
  32. $method_array = explode(".", $method);
  33. $method = '/' . $methodVersion . '/' . $method_array[count($method_array) - 1];
  34. array_pop($method_array);
  35. $method = implode(".", $method_array) . $method;
  36. $url = self::$signUrl . $method;
  37. return $url;
  38. }
  39. public function setFormat($format)
  40. {
  41. if (!in_array($format, YZApiProtocol::allowedFormat()))
  42. throw new Exception('设置的数据格式错误');
  43. $this->format = $format;
  44. return $this;
  45. }
  46. public function setSignMethod($method)
  47. {
  48. if (!in_array($method, YZApiProtocol::allowedSignMethods()))
  49. throw new Exception('设置的签名方法错误');
  50. $this->signMethod = $method;
  51. return $this;
  52. }
  53. private function parseResponse($responseData)
  54. {
  55. $data = json_decode($responseData, true);
  56. if (null === $data) throw new Exception('response invalid, data: ' . $responseData);
  57. return $data;
  58. }
  59. private function buildRequestParams($method, $apiParams)
  60. {
  61. if (!is_array($apiParams)) $apiParams = array();
  62. $pairs = $this->getCommonParams($method);
  63. foreach ($apiParams as $k => $v) {
  64. if (isset($pairs[$k])) throw new Exception('参数名冲突' );
  65. $pairs[$k] = $v;
  66. }
  67. $pairs[YZApiProtocol::SIGN_KEY] = YZApiProtocol::sign($this->appSecret, $pairs, $this->signMethod);
  68. return $pairs;
  69. }
  70. private function getCommonParams($method)
  71. {
  72. $params = array();
  73. $params[YZApiProtocol::APP_ID_KEY] = $this->appId;
  74. $params[YZApiProtocol::METHOD_KEY] = $method;
  75. $params[YZApiProtocol::TIMESTAMP_KEY] = date('Y-m-d H:i:s');
  76. $params[YZApiProtocol::FORMAT_KEY] = $this->format;
  77. $params[YZApiProtocol::SIGN_METHOD_KEY] = $this->signMethod;
  78. $params[YZApiProtocol::VERSION_KEY] = self::VERSION;
  79. return $params;
  80. }
  81. }