|
|
@@ -9,9 +9,9 @@ class WxService extends Service
|
|
|
private $appID = 'wx8c710e2210bee651';
|
|
|
private $appSecret = 'd98ec1fb5b9c9de648f12d1a38db227e';
|
|
|
|
|
|
- public function getToken($cacheKey){
|
|
|
- if(Cache::has($cacheKey)){
|
|
|
- $accessToken = Cache::get($cacheKey);
|
|
|
+ public function getToken(){
|
|
|
+ if(Cache::has($this->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}";
|
|
|
@@ -21,43 +21,53 @@ class WxService extends Service
|
|
|
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/template/send?access_token=$accessToken";
|
|
|
+ // 发送消息
|
|
|
+ $sendURL = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$accessToken";
|
|
|
|
|
|
- $userOpenID = '';
|
|
|
- $template_id = '';
|
|
|
- // 准备模板消息的数据
|
|
|
- $templateData = array(
|
|
|
+ $userOpenID = 'oBmsr5W_l_8nqQQfIMNP5sBzg8ME';
|
|
|
+ // 准备普通文本消息的数据
|
|
|
+ $messageData = array(
|
|
|
'touser' => $userOpenID,
|
|
|
- 'template_id' => $template_id,
|
|
|
- 'data' => array(
|
|
|
- 'code' => array(
|
|
|
- 'value' => $code,
|
|
|
- ),
|
|
|
+ 'msgtype' => 'text',
|
|
|
+ 'text' => array(
|
|
|
+ 'content' => '您的验证码是: '.$code, // 替换为您要发送的验证码
|
|
|
),
|
|
|
);
|
|
|
|
|
|
- $sendData = json_encode($templateData);
|
|
|
+ $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);
|
|
|
- curl_close($ch);
|
|
|
|
|
|
// 处理发送结果
|
|
|
$result = json_decode($response, true);
|
|
|
- if ($result['errcode'] == 0) {
|
|
|
- return [true,'验证码已成功发送到微信用户!'];
|
|
|
+
|
|
|
+ if ($response === false) {
|
|
|
+ $error = curl_error($ch);
|
|
|
+ curl_close($ch);
|
|
|
+ return [false,$error];
|
|
|
} else {
|
|
|
- return [false, '发送验证码时出错:' . $result['errmsg']] ;
|
|
|
+ curl_close($ch);
|
|
|
+ if ($result['errcode'] == 0) {
|
|
|
+ return [true,'验证码已成功发送到微信用户!'];
|
|
|
+ } else {
|
|
|
+ return [false, '发送验证码时出错:' . $result['errmsg']] ;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|