appId = $appId; $this->appSecret = $appSecret; } /** * 发送消息 * @Author:lun * @Date:2024-02-18 15:45 * @Copyright:copyright(c)2023 广东七件事集团 * @param $url * @param $device_id * @param $msg_type * @param $message * @param $pay_money * @return array */ public function suona($url, $device_id, $msg_type, $message = '', $pay_money = 0) { if (is_array($device_id)) { $device_ids = $device_id; } else { $device_ids = [$device_id]; } if ($msg_type == 0) { //自定义文本消息 $params = json_encode([ 'device_ids' => $device_ids, 'msg_type' => $msg_type, 'message' => $message, ]); } else { //系统自带的文本信息 $params = json_encode([ 'device_ids' => $device_ids, 'msg_type' => $msg_type, 'param1' => $pay_money, ]); } $array = $this->curl_post($params, $this->generateHeader(), $url); return $array; } //curl header封装 public function generateHeader() { $millisecond = $this->getMillisecond();//为服务端时间前后5分钟内可以访问 $nonceStr = $this->getNonceStr(16);//10到50位字符串 return [ 'appId:' . $this->appId, 'signature:' . $this->getSignature($this->appId, $this->appSecret, $nonceStr, $millisecond), 'nonceStr:' . $nonceStr, 'timestamp:' . $millisecond, 'Content-Type:application/json', ]; } //计算请求签名值 private function getSignature($appId, $appSecret, $nonceStr, $millisecond) { $s = $appId . '_' . $appSecret . '_' . $nonceStr . '_' . $millisecond; return strtoupper(md5($s)); } //模拟发送POST请求 /** * 模拟发送POST 方式请求 * @param $url * @param $data * @param $projectId * @param $signature * @return array */ // public function http_post_data($data, $header, $url) // { // $ch = curl_init(); // curl_setopt($ch, CURLOPT_POST, 1); // curl_setopt($ch, CURLOPT_URL, $url); // curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // curl_setopt($ch, CURLOPT_HTTPHEADER, $header); // ob_start(); // curl_exec($ch); // $return_content = ob_get_contents(); // ob_end_clean(); // $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); // return array($return_code, $return_content); // } public function curl_post($postdata = '', $header = '', $url, $options = array()) { $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); if (!empty($options)) { curl_setopt_array($ch, $options); } $data = curl_exec($ch); curl_close($ch); return $data; } /** * * 产生随机字符串,不长于50位 * @param int $length * @return string */ private function getNonceStr($length = 50) { $chars = "abcdefghijklmnopqrstuvwxyz0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } //获取当前时间戳(毫秒级) private function getMillisecond() { list($s1, $s2) = explode(' ', microtime()); return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); } }