ApiProtocol.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace extend\api;
  3. class ApiProtocol {
  4. const APP_ID_KEY = 'app_key';
  5. const METHOD_KEY = 'method';
  6. const TIMESTAMP_KEY = 'timestamp';
  7. const FORMAT_KEY = 'format';
  8. const VERSION_KEY = 'version';
  9. const SIGN_KEY = 'sign';
  10. const SIGN_METHOD_KEY = 'sign_method';
  11. const TOKEN_KEY = 'access_token';
  12. const ALLOWED_DEVIATE_SECONDS = 600;
  13. const ERR_SYSTEM = -1;
  14. const ERR_INVALID_APP_ID = 40001;
  15. const ERR_INVALID_APP = 40002;
  16. const ERR_INVALID_TIMESTAMP = 40003;
  17. const ERR_EMPTY_SIGNATURE = 40004;
  18. const ERR_INVALID_SIGNATURE = 40005;
  19. const ERR_INVALID_METHOD_NAME = 40006;
  20. const ERR_INVALID_METHOD = 40007;
  21. const ERR_INVALID_TEAM = 40008;
  22. const ERR_PARAMETER = 41000;
  23. const ERR_LOGIC = 50000;
  24. public static function sign($appSecret, $params, $method = 'md5') {
  25. if (!is_array($params)) $params = array();
  26. ksort($params);
  27. $text = '';
  28. foreach ($params as $k => $v) {
  29. $text .= $k . $v;
  30. }
  31. return self::hash($method, $appSecret . $text . $appSecret);
  32. }
  33. private static function hash($method, $text) {
  34. switch ($method) {
  35. case 'md5':
  36. default:
  37. $signature = md5($text);
  38. break;
  39. }
  40. return $signature;
  41. }
  42. public static function allowed_sign_methods() {
  43. return array('md5');
  44. }
  45. public static function allowed_format() {
  46. return array('json');
  47. }
  48. }