SuonaLogic.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace addons\Store\common\logic\order;
  3. class SuonaLogic
  4. {
  5. public $appId = '';
  6. public $appSecret = '';
  7. public function __construct($appId, $appSecret)
  8. {
  9. $this->appId = $appId;
  10. $this->appSecret = $appSecret;
  11. }
  12. public function suona($url, $pay_money, $device_id, $msg_type)
  13. {
  14. $params = json_encode([
  15. 'device_ids' => [$device_id],
  16. 'msg_type' => $msg_type,
  17. 'param1' => $pay_money,
  18. ]);
  19. \Yii::info('云喇叭请求:'.$params);
  20. $response = $this->curl_post($params, $this->generateHeader(), $url);
  21. \Yii::info('云喇叭响应:'.json_encode($response));
  22. return ['params' => $params, 'response' => is_array($response) ? $response : json_decode($response, true)];
  23. }
  24. //curl header封装
  25. public function generateHeader()
  26. {
  27. $millisecond = $this->getMillisecond();//为服务端时间前后5分钟内可以访问
  28. $nonceStr = $this->getNonceStr(16);//10到50位字符串
  29. return [
  30. 'appId:' . $this->appId,
  31. 'signature:' . $this->getSignature($this->appId, $this->appSecret, $nonceStr, $millisecond),
  32. 'nonceStr:' . $nonceStr,
  33. 'timestamp:' . $millisecond,
  34. 'Content-Type:application/json',
  35. ];
  36. }
  37. //计算请求签名值
  38. private function getSignature($appId, $appSecret, $nonceStr, $millisecond)
  39. {
  40. $s = $appId . '_' . $appSecret . '_' . $nonceStr . '_' . $millisecond;
  41. return strtoupper(md5($s));
  42. }
  43. //模拟发送POST请求
  44. /**
  45. * 模拟发送POST 方式请求
  46. * @param $url
  47. * @param $data
  48. * @param $projectId
  49. * @param $signature
  50. * @return array
  51. */
  52. // public function http_post_data($data, $header, $url)
  53. // {
  54. // $ch = curl_init();
  55. // curl_setopt($ch, CURLOPT_POST, 1);
  56. // curl_setopt($ch, CURLOPT_URL, $url);
  57. // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  58. // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  59. // ob_start();
  60. // curl_exec($ch);
  61. // $return_content = ob_get_contents();
  62. // ob_end_clean();
  63. // $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  64. // return array($return_code, $return_content);
  65. // }
  66. public function curl_post($postdata = '', $header = '', $url, $options = array())
  67. {
  68. $ch = curl_init($url);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  70. curl_setopt($ch, CURLOPT_POST, 1);
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  72. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  73. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  74. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  75. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  76. if (!empty($options)) {
  77. curl_setopt_array($ch, $options);
  78. }
  79. $data = curl_exec($ch);
  80. curl_close($ch);
  81. return $data;
  82. }
  83. /**
  84. *
  85. * 产生随机字符串,不长于50位
  86. * @param int $length
  87. * @return string
  88. */
  89. private function getNonceStr($length = 50)
  90. {
  91. $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
  92. $str = "";
  93. for ($i = 0; $i < $length; $i++) {
  94. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  95. }
  96. return $str;
  97. }
  98. //获取当前时间戳(毫秒级)
  99. private function getMillisecond()
  100. {
  101. list($s1, $s2) = explode(' ', microtime());
  102. return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
  103. }
  104. }