微信公众号登录(测试号)
1.https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=614565808&lang=zh_CN
基本设置token验证
define("TOKEN", "fmyinpin123456"); $echoStr = $_GET["echostr"]; //valid signature , option if(WechatCallbackApiTest::checkSignature()){ echo $echoStr; exit; } <?php /** * Created by Fm. * User: Arvin * Date: 2019/1/21 * Time: 15:00 */ namespace app\api\libs; class WechatCallbackApiTest { public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } } public static function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } }
2.https://mp.weixin.qq.com/cgi-bin/settingpage?t=setting/function&action=function&token=614565808&lang=zh_CN
网页授权域名回调设置
3.https://mp.weixin.qq.com/cgi-bin/frame?t=advanced/dev_tools_frame&nav=10049&token=614565808&lang=zh_CN
公众平台测试号设置同上
接口配置信息/修改
JS接口安全域名/修改
网页授权获取用户基本信息/修改
public function index(){ $appid =$this->appid; $redirect_uri = urlencode($this->redirect_uri); $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; header("Location:".$url); exit; } public function http_curl($url,$type='get',$res='json',$arr='') { //1.初始化curl $ch = curl_init(); //2.设置curl的参数 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($type == 'post') { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $arr); } //3.采集 $output = curl_exec($ch); //4.关闭 curl_close($ch); if ($res == 'json') { return json_decode($output, true); } } // public function getUserOpentId(){ //回调地址会传回一个code,则我们根据code去获取openid和授权获取到的access_token $code = $_GET['code']; $appid = $this->appid; $secret =$this->appsecret; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code"; $res = $this->http_curl($url); $access_token = $res['access_token']; $getopenid = $res['openid']; //获取用户授权信息 $urltoc = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$getopenid."&lang=zh_CN"; $resinfos = $this->http_curl($urltoc); print_r($res); print_r($resinfos); die; $openid = $resinfos['openid']; $check_member = Db::name("member")->where('openid',$openid)->find(); if(empty($check_member)){ //首次进入,则获取用户信息,插入数据库 $resinfo['openid'] = $openid; $insert_data = [ 'openid' => $openid, 'create_time' => time() ]; Db::name("member")->insert($insert_data); $userId = Db::name('member')->getLastInsID(); Session::set('wx_member_info', $resinfo); $this->redirect('home/index/index_html'); }else{ //说明是已经是公众号成员,则调用用户信息存到session即可 $wx_member_info = Db::name('member')->where("openid",$openid)->find(); Session::set('wx_member_info', $wx_member_info); $this->redirect('home/index/index_html'); } }