appID)){ $accessToken = Cache::get($this->appID); }else{ // 获取访问令牌(Access Token) $apiURL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appID}&secret={$this->appSecret}"; $response = file_get_contents($apiURL); $result = json_decode($response, true); if(isset($result['errcode'])){ return [false,"错误码:{$result['errcode']} 详细信息:{$result['errmsg']}"]; } $accessToken = $result['access_token']; $expiresIn = ($result['expires_in']-60)/60; Cache::add($this->appID,$accessToken,$expiresIn); } return [true,$accessToken]; } //发送普通消息 public function sendToWx($accessToken,$code){ // 发送消息 $sendURL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$accessToken"; $userOpenID = 'oBmsr5W_l_8nqQQfIMNP5sBzg8ME'; // 准备普通文本消息的数据 $messageData = array( 'touser' => $userOpenID, 'msgtype' => 'text', 'text' => array( 'content' => '您的验证码是: '.$code, // 替换为您要发送的验证码 ), ); $sendData = json_encode($messageData); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $sendURL); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $sendData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $response = curl_exec($ch); // 处理发送结果 $result = json_decode($response, true); if ($response === false) { $error = curl_error($ch); curl_close($ch); return [false,$error]; } else { curl_close($ch); if ($result['errcode'] == 0) { return [true,'验证码已成功发送到微信用户!']; } else { return [false, '发送验证码时出错:' . $result['errmsg']] ; } } } }