当前位置: 首页 > 后端技术 > PHP

用户关注微信公众号,推送数据处理

时间:2023-03-29 23:36:28 PHP

model=newWxModel();}publicfunctionindex(){if(!isset($_GET['echostr'])){$this->responseMsg();}else{$this->valid();}/*$appid=config('wx.wx_app_id');$secret=config('wx.wx_app_secret');if(!$this->request->get('code','')){$redirect_uri=urlEncode('https://test.yishaxiyi.com/storeapi/wxUserInfo');$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";header("位置:$url");}else{$code=$this->request->get('code','');$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";$return=json_decode(curl_get($url),true);$access_token=$return['access_token'];$openid=$return['openid'];$user_info=json_decode(curl_get("https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"),true);$user=Db::name('wx_user')->where('wx_openid','=',$openid)->find();if($user){}else{$post['wx_openid']=$openid;$post['code']=$code;$post['unionid']=isset($user_info['unionid'])?$user_info['unionid']:'';$post['nickname']=isset($user_info['nickname'])?$user_info['昵称']:'';$post['sex']=isset($user_info['sex'])?$user_info['sex']:'';$post['avatar']=isset($user_info['headimgurl'])?$user_info['headimgurl']:'';$post['create_time']=time();返回$this->model->addData($post);}}*/}publicfunctionvalid(){$echoStr=$_GET["echostr"];如果($this->checkSignature()){echo$echoStr;出口;}}私有函数checkSignature(){$signature=$_GET["signature"];$timestamp=$_GET["时间戳"];$nonce=$_GET["nonce"];$token=config('wx.token');$tmpArr=array($token,$timestamp,$nonce);排序($tmpArr);$tmpStr=内爆($tmpArr);$tmpStr=sha1($tmpStr);如果($tmpStr==$signature){返回真;}else{返回错误;}}publicfunctionresponseMsg(){$postStr=file_get_contents("php://input");如果(!empty($postStr)){$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);$RX_TYPE=trim($postObj->MsgType);switch($RX_TYPE){case"event":$result=$this->receiveEvent($postObj);}休息;}回声$重新苏尔特;}else{回声“”;出口;}}私有函数receiveEvent($object){$content="";switch($object->Event){case"subscribe"://关注事件$content="欢迎关注马上洗,更多功能保持继续发布中,敬请期待...";break;case"unsubscribe"://取消订阅事件$content="";break;}$result=$this->transmitText($object,$内容);返回$result;}私有函数transmitText($object,$content){$textTpl="%s";$result=sprintf($textTpl,$object->FromUserName,$object->ToUserName,time(),$content);$res=$this->model->where('wx_openid',$object->FromUserName)->find();if($res){return$result;}$appid=config('wx.wx_app_id');$secret=config('wx.wx_app_s秘密');$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";$return=json_decode(curl_get($url),true);$access_token=$return['access_token'];$getUserInfoUrl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$object->FromUserName&lang=zh_CN";$return=json_decode(curl_get($getUserInfoUrl),true);$this->model->insert(['subscribe'=>$return['subscribe'],'unionid'=>$return['unionid'],'wx_openid'=>$return['openid'],'nickname'=>$return['nickname'],'sex'=>$return['sex'],'avatar'=>$return['headimgurl'],'create_time'=>time(),]);返回$结果;}}