$appId = 'wx7d44aa9edbaae28e';
$appSecret = 'f2c86c9b3bd1db6e816ac218dfe75076';
// 获取推送过来的加密参数
$data = file_get_contents('php://input');
// $this->db->insert('t_test',array('data'=>$data));
// 解析JSON数据
$jsonData = $this->XmlToArr($data);
if($jsonData['Event'] != "subscribe"){
// 非用户关注
return false;
}
// $this->db->insert('t_test',array('data'=>json_encode($jsonData)));
// 获取加密参数
$openid = $jsonData['FromUserName']; // 公众号openid
// 获取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
$response = file_get_contents($url);
$accessToken = json_decode($response, true)['access_token'];
// 解密参数
$urls = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$accessToken}&openid={$openid}&lang=zh_CN&appid={$appId}";
$responses = file_get_contents($urls);
$this->db->insert('t_test',array('data'=>$responses));
$decryptedData = json_decode($responses, true);
// 获取解密后的用户参数
$openId = $decryptedData['openid'];
$unionid = $decryptedData['unionid'];
if(!empty($openId) && !empty($unionid)){
$this->db->update('t_user',array('wechat_openid'=>$openId),array('unionid'=>$unionid));
}
网友评论