open_query.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace mtopcard;
  3. class open_query
  4. {
  5. private const OPENAPI_URL = 'https://openapi.xyzshops.cn/api.php/CardInfo/query';
  6. private const APPID = 'ea97586b4aa0c141e4456912f3325f7f';
  7. private const SECURE = '92c51d28e3405c923decf1b333cec3ce';
  8. public function validate($card_no)
  9. {
  10. $params = ['cardno' => $card_no, 'appid' => self::APPID];
  11. $params['sign'] = $this->sign($params);
  12. $resp = http_request(self::OPENAPI_URL,$params);
  13. if(empty($resp)) {
  14. return [false,[]];
  15. }
  16. $resp = json_decode($resp, true);
  17. if ($resp['code'] == 200) {
  18. $result = $resp['data'];
  19. return [true, $result];
  20. } else {
  21. return [false, []];
  22. }
  23. }
  24. private function sign($input)
  25. {
  26. $body = $this->body($input);
  27. $body .= "&key=" . self::SECURE;
  28. return md5($body);
  29. }
  30. private function check_empty($value)
  31. {
  32. if (!isset($value))
  33. return true;
  34. if ($value === null)
  35. return true;
  36. if (trim($value) === "")
  37. return true;
  38. return false;
  39. }
  40. private function body($params)
  41. {
  42. ksort($params);
  43. $body = "";
  44. $i = 0;
  45. foreach ($params as $k => $v)
  46. {
  47. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  48. {
  49. if ($i == 0) {
  50. $body .= "$k" . "=" . urlencode($v);
  51. } else {
  52. $body .= "&" . "$k" . "=" . urlencode($v);
  53. }
  54. $i++;
  55. }
  56. }
  57. return $body;
  58. }
  59. }