微信根据openid发送消息,获取用户详情,获取access

    xiaoxiao2026-08-13  12

    纯属娱乐,有什么不对的地方请多多指教

    <?php

    session_start();//开启session缓存机制 class WxSendMessage{ private  $Appid;//应用id private  $Appsecret;//应用密钥 private  $WxGetUrl ;//微信Get请求url private  $WxPostUrl;//微信Post请求url private  $MessageID;//信息模板id; private  $access_token;//微信唯一密钥 private  $state;//返回状态 private  $message;//返回信息 private  $result = array();//返回内容 public function __construct($appid,$appsecret){ $this->Appid = $appid; $this->Appsecret = $appsecret;  } /** * Get方式实现 * @return [array]  $output   [返回数据] */ private function curl_Get(){ try{ $ch = curl_init();//初始化 curl_setopt($ch, CURLOPT_URL, $this->WxGetUrl);   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   curl_setopt($ch, CURLOPT_HEADER, 0); $output = curl_exec($ch); curl_close($ch); return $output; }catch(Exception $e){ return false; } } /** * [curl_Post Post方式实现] * @param  [array] $data [发送数据] * @return [array]       [返回数据] */ private function curl_Post($data){ try{ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->WxPostUrl);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); $output = curl_exec($ch);       curl_close($ch); return $output; }catch(Exception $e){ return false; } } /** * [getAccontToken 获取微信access_token唯一凭证] * @return [array] [返回数据信息] */ public function getAccontToken(){ $this->state = 0;//0:失败,1:成功; $this->message = "获取微信的请求数据失败"; $result = array();//返回数据 $this->WxGetUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->Appid."&secret='".$this->Appsecret."'"; if(!$_SESSION['time'])$_SESSION['time'] = 0; if(!$_SESSION['access_token'] || time()-$_SESSION['time']>7200){ $res = $this->curl_Get(); if($res){ $res = json_decode($res,true); if($res['errcode']){ $this->message = $res['errmsg']; }else{ $this->state = 1; $this->message = "获取微信的请求数据成功"; $_SESSION['time'] = time(); $this->result['access_token'] = $this->access_token = $_SESSION['access_token'] = $res['access_token']; } } }else{ $this->state = 1; $this->message = "获取微信的请求数据成功"; $this->result['access_token'] = $this->access_token = $_SESSION['access_token'] ; } $this->getResultInfo();   return $this->result; } /** * [sendMessageByOpenid 向openid用户发送信息] * @param  [string] $openid    [接收者openid] * @param  [string] $messageid [消息类型id] * @param  [array] $data      [发送数据] * @return [array]            [返回发送状态] */ public function sendMessageByOpenid($openid,$messageid,$datas){ $this->state = 0;//状态:0:失败;1:成功; $this->message = "微信发送信息失败"; $result = array(); $this->getAccontToken(); if(!$this->access_token){ $this->message = "获取微信access_token失败"; $this->getResultInfo(); return $this->result; } if(!$openid || !$messageid || !$datas){ $this->message = "缺少微信发送信息必填参数"; $this->getResultInfo(); return $this->result; } $this->WxPostUrl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$this->access_token; $data['touser'] = $openid; $data['messageid'] = $messageid; $data['data'] = $datas; $res = $this->curl_Post($data); if($res){ $res = json_encode($res,true); if($res['errcode'] == 0){ $this->result['msgid'] = $res['msgid']; $this->state = 1; $this->message = '微信发送信息成功'; } } return $this->result; } /** * 返回信息 */ public function getReturnInfo(){ $this->result['message'] = $this->message; $this->result['state']   = $this->state; } /** * [getUserInfoByOpenid 根据openid获取用户信息] * @param  [string] $openid [用户openid] * @return  */ public function getUserInfoByOpenid($openid){ $this->state = 0;//状态:0:失败;1:成功; $this->getAccontToken(); if(!$this->access_token){ $this->message = '获取微信access_token失败'; $this->getReturnInfo(); return $this->result; } if(!$openid){ $this->message = '获取微信openid失败'; $this->getReturnInfo(); return $this->result; } $this->WxGetUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token='".$this->access_token."'&openid='".$openid."'&lang=zh_CN"; $res = $this->curl_Get(); if($res){ $this->state = 1; $this->message = '获取用户信息成功'; $this->result['data'] = $res; } $this->getReturnInfo(); return $this->result; } }

    ?>

    转载请注明原文地址: https://ju.6miu.com/read-1311143.html
    最新回复(0)