open_query.php 1.7 KB

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