sendTempMessage(); } } /** * 推送模板消息 * @return bool */ public function sendTempMessage() { $wechatUtil = new WechatUtil($this->_appId, $this->_appSecrect); $accessToken = $wechatUtil->getAccessToken(); if(!empty($accessToken)) { foreach ($this->_openidList as $openid) { $postData = '{ "touser":"'.$openid.'", "template_id":"UN3cXcr7kYtt11AJGe-YpCOdndnQ7w3lDYdyOV3J3IQ", "url":"", "topcolor":"#FF683F", "data":{ "first":{ "value":"警告提醒!游戏在线人数小于30人,请打开游戏确认能否正常登录!", "color":"#ff510" }, "keyword1":{ "value":"管理员", "color":"#ff510" }, "keyword2":{ "value":"'.date('Y-m-d H:i:s',time()).'", "color":"#ff510" }, "remark":{ "value":"服务器、数据库、php或者nginx运行是否异常", "color":"#ff510" } } }'; $response = $this->curlJson($this->_sendTempUrl.$accessToken, $postData); oo::logs()->todebug('response:'.json_encode($response) , 'wechat.php'); } } return true; } /** * 发送客服消息提醒 * @return bool */ public function sendCustomMessage() { $wechatUtil = new WechatUtil($this->_appId, $this->_appSecrect); $accessToken = $wechatUtil->getAccessToken(); if(!empty($accessToken)) { foreach ($this->_openidList as $openid) { $postData = '{ "touser":"'.$openid.'", "msgtype":"text", "text": { "content":"警告提醒!游戏在线人数小于30人,请打开游戏确认能否正常登录!" } }'; $response = $this->curlJson($this->_sendCustomUrl.$accessToken, $postData); oo::logs()->todebug('response:'.json_encode($response) , 'wechat.php'); } } return true; } public function curlJson($url, $dataJson) { $ch = curl_init($url); //请求的URL地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);//$data JSON类型字符串 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($dataJson))); $data = curl_exec($ch); return $data; } }