微信开发已经是现在phper必须掌握的基础技术了。其实做过微信开发的都知道,微信接口很强大,也很容易做。下面我们来看一个微信自动登录注册的例子。php微信扫码PC自动登录注册接口作用域为snsapi_userinfo。其中一个微信登录是网页授权登录,一个是微信联合登录网页授权登录:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html微信联合登录:https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN1.首先在微信链接上生成一个带有logo的二维码。比如链接是https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect\_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'我们可以在状态条上做,因为你传入的状态和微信返回的内容,可以作为服务器和微信段的标识:publicfunctioncreatqrAction(){if($_GET['app']){$wtoken=$_COOKIE['wtoken'];$postdata=$_SESSION['w_state'];if($wtoken){$postdata=$wtoken;}includeCONFIG_PATH.'phpqrcode/'.'phpqrcode.php'$sh=$this->shar1();$value="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx138697ef383a9167&redirect_uri=http://www.xxx.net/login/wcallback&response_type=code&scope=snsapi_userinfo&state=".$postdata."&connect_redirect=1#wechat_redirect";$errorCorrectionLevel="L";$matrixPointSize="5";QRcode::png($value,false,$errorCorrectionLevel,$matrixPointSize);}}生成于此time二维码状态是标识,phpqrcode可以在文末下载,所以我们在wcallback方法中设置回调地址http://www.xxx.net/login/wcallback处理数据,插入用户生成session,跳转到登录,PC端可以设置几秒ajax请求服务器,获取到状态后,即可实现调整,在微信浏览器中处理后,即可关闭窗口微信js可以实现:document.addEventListener('WeixinJSBridgeReady',functiononBridgeReady(){WeixinJSBridge.call('closeWindow');},false);也可以授权登录成功跳转到微信服务号关注页面:header("位置:weixin://profile/gh_a5e1959f9a4e");wcallback方法处理登录$code=$_GET['code'];$state=$_GET['state'];$setting=includeCONFIG_PATH。'setting.php'$appid=$setting['weixin']['appid'];$appsecret=$setting['weixin']['appsecret'];if(emptyempty($code))$this->showMessage('授权失败');try{$token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'$token=json_decode($this->https_request($token_url));}catch(Exception$e){print_r($e);}if(isset($token->errcode)){echo'错误:'.$token->errcode;echo'错误信息:'.$token->errmsg;exit;}$access_token_url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;//转换为对象$access_token=json_decode($this->https_request($access_token_url));if(isset($access_token->errcode)){echo'Error:'.$access_token->errcode;echo'错误信息:'.$access_token->errmsg;exit;}$user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'//转换为对象$user_info=json_decode($this->https_request($user_info_url));if(isset($user_info->errcode)){echo'error:'.$user_info->errcode;echo'错误信息:'.$user_info->errmsg;exit;}//打印用户信息//echo''//print_r($user_info);//echo''在phpqrcode类库下这里没有提供。可以搜索下载magento微信扫码网站自动登录https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN查看授权后界面call(UnionID),不难发现,填写回调地址,用户确认登录PC后,即可跳转获取UnionID。方法publicfunctionwcallbackAction(){$code=$_GET['code'];$state=$_GET['state'];$setting=includeCONFIG_PATH。'setting.php';$appid=$setting['weixin']['appid'];$appsecret=$setting['weixin']['appsecret'];if(emptyempty($code))$this->showMessage('授权失败');try{$token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';$token=json_decode($this->https_request($token_url));}catch(异常$e){print_r($e);}if(isset($token->errcode)){echo'
Error:
'.$token->errcode;echo'Error:
'.$token->errmsg;exit;}$access_token_url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;//转换为对象$access_token=json_decode($this->https_request($access_token_url));if(isset($access_token->errcode)){echo'错误:
'.$access_token->errcode;echo'错误信息:
'.$access_token->errmsg;exit;}$user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';//转换为对象$user_info=json_decode($this->https_request($user_info_url));if(isset($user_info->errcode)){echo'Error:
'.$user_info->errcode;echo'错误信息:
'.$user_info->errmsg;exit;}//打印用户信息//echo'';//print_r($user_info);//echo'';//获取unionid$uid=$user_info->unionid;}//用户操作处理分为再次登录和首次登录$sql="selecth_user_idfromdtb_user_bindedast1leftjoindtb_user_weixinast2ont1.u_id=t2.ID其中t1.u_type='".User::$arrUtype['weixin_num_t']."'和t2.openid='$user_info->unionid'";$h_user_id=Core_Db::getOne($sql);if(!emptyempty($h_user_id)){//微信号再次登录}}{//微信号首次登录}
