request->filter('trim,strip_tags,htmlspecialchars'); } public function index(){ echo '测试连接成功'; } /** *1.服务器接口,微信公众平台填写的url * http://域名/控制器/link */ public function link(){ $echostr=$_GET['echostr'];//微信服务器提供的 随机字符串 if ($this->check()){//验证签名是否正确 echo $echostr; exit; } } /** * 2.验证签名 */ public function check(){ $signature=$_GET['signature']; //微信服务器提供的 微信加密签名 $timestamp=$_GET['timestamp']; //微信服务器提供的 时间戳 $nonce=$_GET['nonce']; //微信服务器提供的 随机数 $token='z9EGslrxPpbicuy48mkw'; //自己定义的 Token $tmpArr = array($token,$timestamp,$nonce);//数组 sort($tmpArr); //排序 $tmpstr=implode($tmpArr); //数据转字符串 $tmpstr=sha1($tmpstr); //字符串加密 if ($tmpstr==$signature){ return true; }else{ return false; } } /** * 获取access_token存进数据库 */ public function access_token(){ $token = Db::name("v_access_token")->find(1); $date = date('Y-m-d H:i:s'); if (strtotime($token['addtime']) > strtotime($date)){ return $token['access_token']; }else{ $appid = "你的appid"; $appsecret = "你的appsecret "; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}"; $rt = $this->request_get($url); $data['access_token']=$rt['access_token']; $data['addtime']= date("Y-m-d H:i:s", strtotime("$date +60 min")); $rt =DB::name("v_access_token")->where("id='1'")->save($data); if ($rt){ $token = Db::name("v_access_token")->find(1); return $token['access_token']; }else{ return "获取access_token错误"; } } } /** * 3.发送http请求,并返回数据 * @param $url * @return mixed */ public function request_get($url){ $curl = curl_init();// 1. 初始化一个 cURL 对象 curl_setopt($curl,CURLOPT_URL,$url);// 2.设置你需要抓取的URL curl_setopt($curl,CURLOPT_HEADER,0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1 );// 3.https必须加这个,不加不好使(不多加解释,东西太多了 curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); $res = curl_exec($curl);// 5. 运行cURL,请求网页 curl_close($curl);// 6. 关闭URL请求 $json_obj = json_decode($res,true); return $json_obj; } public function user(){ //1.用户点击静默授权链接 获取用户的code $code = input("code"); //2.通过code换取网页授权access_token $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appid&secret=$this->appsecret&code={$code}&grant_type=authorization_code"; $rt = $this->request_get($url); //拿着access_token换取用户信息 if (!empty($rt['access_token'])) { Session('access_token', $rt['access_token'], 7200); Session('openid', $rt['openid'], 7200); } $access_token = Session('access_token'); $openid = Session('openid'); //3.获取用户基本信息 $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}&lang=zh_CN"; $user_url = $this->request_get($url); $openid = $user_url['openid']; if($openid){ $user = Db::name('v_user')->where("openid='$openid'")->find(); if (!$user){ $data['openid'] = $user_url['openid'];//用户openid $data['nickname'] = $user_url['nickname'];//用户名字 $data['headimgurl'] = $user_url['headimgurl'];//用户头像 $data['sex'] = $user_url['sex'];//用户性别 $data['addtime'] = date('Y-m-d H:i:s'); Db::name('v_user')->add($data); }//数据库没有用户信息添加到数据库mn_user用户表 }else{ $this->error('请使用手机进入',U('index')); } } }