慢慢的你会发现除了获取参数的地方不一样,登录流程都差不多,可能是接口路径啥的不太一致

但是获取用户信息,出奇的一至:https://api.weixin.qq.com/sns/userinfo
获取openid的却不一样
小程序的是:https://api.weixin.qq.com/sns/jscode2session

扫码的、公众号的、app的是:https://api.weixin.qq.com/sns/oauth2/access_token

pc扫码

开始解说
先来个公共的curl函数

  function geturl($url){$headerArray =array("Content-type:application/json;","Accept:application/json");$ch = curl_init();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);curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);$output = curl_exec($ch);curl_close($ch);$output = json_decode($output,true);return $output;
}

1、获取二维码

$appid="wx1e1275cb2e70e359";//扫码后回调地址$url=urlencode("http://www.taianyuannongmu.com/business/login/weixin2");$headerurl="https://open.weixin.qq.com/connect/qrconnect?appid=".$appid;$headerurl.="&redirect_uri=".$url;$headerurl.="&response_type=code&scope=snsapi_login&state=wechat_redirect"header("Location:".$headerurl);

2、成功回调获取openid

 $appid="";$AppSecret="";$url=urlencode("http://www.taianyuannongmu.com/business/login/weixin2");$weixinurl= "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid;$weixinurl.="&secret=".$AppSecret;$weixinurl.="&code=".$_GET["code"];$weixinurl.="&grant_type=authorization_code";//这里是curl$res=$this->geturl($weixinurl);$_SESSION["access_token"]=$res['access_token'];$_SESSION["openid"]=$res['openid'];

3.获取用户信息

 $access_token=$_SESSION["access_token"];$openid=$_SESSION["openid"];/*获取用户信息*/$get_user_info_url='https://api.weixin.qq.com/sns/userinf';$get_user_info_url.='?access_token='.$access_token;$get_user_info_url.='&openid='.$openid.'&lang=zh_CN'; $weixin3res= $this->geturl($get_user_info_url);

剩下的就是逻辑代码处理

app登录


前端调取授权
后端根据前端的code获取OPENID和身份信息

先来个公共的curl函数

  function geturl($url){$headerArray =array("Content-type:application/json;","Accept:application/json");$ch = curl_init();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);curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);$output = curl_exec($ch);curl_close($ch);$output = json_decode($output,true);return $output;
}
```php
$appid = "";         //开发平台申请
$appsecret =  "";        //开发平台申请
$code = $_GET['code'];   //安卓端提供用户同意登入后的code//认证
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code ."&grant_type=authorization_code";//调用微信api$rs = $this -> geturl($url);
if(!$rs)$this -> error('获取OPENID失败');$rt = json_decode($rs, 1);
if($rt['errcode'])$this -> error('授权失败:'.$rt['errmsg']);// 拉取用户信息
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$rt['access_token']."&openid=".$rt['openid']."&lang=zh_CN ";$wechat_info = $this -> geturl($url);
if(!$wechat_info)$this -> error('获取用户资料失败:CURL '.$http -> errmsg);
$wechat_info = json_decode($wechat_info, 1);
if($wechat_info['errcode']){$this -> error("获取用户资料失败".$wechat_info['errmsg']);
}$user_info = array("headimg"=>$wechat_info['headimgurl'], //头像"nickname"=>$wechat_info['nickname'],   //昵称"sex"=>$wechat_info['sex'],             //性别"openid"=>$wechat_info['openid'],       //app唯一"unionid"=>$wechat_info['unionid']       //微信内部唯一,小程序, 公众号, web, 移动应用都是一致的
);

小程序

1、前端获取到code后传给后端
2、通过code获取openid 接口路径https://api.weixin.qq.com/sns/jscode2session?
3、通过openid获取用户信息 接口路径 https://api.weixin.qq.com/sns/userinfo?

整合代码

 <?php
namespace real;
/***  小程序登录类*/
class WeChatMiniProgram
{public $APPID="";public $SECRET="";// 1、前端获取到 code 后传给后端
// 2、通过 code 获取 openid
public function getOpenid($code){$httpcurl="https://api.weixin.qq.com/sns/jscode2session?";$httpcurl.="appid=".$this->APPID;$httpcurl.="&secret=".$this->SECRET;$httpcurl.="&js_code=".$code;$httpcurl.="&grant_type=authorization_code";$zz=$this->geturl($httpcurl);return $zz;
}//getcurl
public function geturl($url){$headerArray =array("Content-type:application/json;","Accept:application/json");$ch = curl_init();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);curl_setopt($ch,CURLOPT_HTTPHEADER,$headerArray);$output = curl_exec($ch);//关闭URL请求if($output === FALSE ){echo "CURL Error:".curl_error($ch);} curl_close($ch);$output = json_decode($output,true);return $output;
}
}

公众号

写代码前先得服务器配置

服务器配置验证用

        $timestamp = $_GET['timestamp'];        $nonce     = $_GET['nonce'];$token     = 'weixuegu';$signature = $_GET['signature'];$array     = array($timestamp,$nonce,$token);sort($array);//将排序后的三个参数拼接之后参数拼接之后进行sha1加密$tmpstr    = implode('',$array);$tmpstr    = sha1($tmpstr); //  $file_write = file_put_contents("./".time().'.text',$signature.",".json_encode($tmpstr));//将加密后的字符串与signature进行对比;if($tmpstr == $signature && isset($_GET['echostr'])){echo $_GET['echostr'];exit;}else{$this->responseMsg();}

去权限设置,授权登录,设置域名


来我们解释代码了
微信公众号第一步:获取code

     $appid= $this->appid;$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=";//appid$url.=$appid;$url.="&redirect_uri=";//  这里是回调的域名和地址$url.="http://".$_SERVER['SERVER_NAME']."/api/Weixinlogin";//下面的都是固定参数$url.="&response_type=code";$url.="&scope=snsapi_userinfo";$url.="&state="."123";$url.="#wechat_redirect"; header("location: ".$url) ; exit();

微信公众号第二步:根据code获取openid

         $appid= $this->appid;$secret= $this->secret; //第一步返回的code$code = $_GET['code'];$get_token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid=';$get_token_url.=$appid;$get_token_url.='&secret=';$get_token_url.=$secret;$get_token_url.='&code=';$get_token_url.=$code;$get_token_url.='&grant_type=authorization_code'; //通过curl获取到数据access_token和openid$curl = curl_init(); // 启动一个CURL会话curl_setopt($curl, CURLOPT_URL, $get_token_url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// 跳过证书检查curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); if($res === FALSE ){echo "CURL Error:".curl_error($curl);}$json_obj=json_decode($res,true);$_SESSION["access_token"]=$json_obj['access_token'];$_SESSION["openid"]=$json_obj['openid'];

微信公众号第四步:根据openid获取用户信息

这里不用第三步,没必要哈

$access_token=$_SESSION['access_token'];$openid=$_SESSION["openid"];$get_user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';$curl1 = curl_init(); // 启动一个CURL会话curl_setopt($curl1, CURLOPT_URL, $get_user_info_url);curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);// 跳过证书检查curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false); $res1 = curl_exec($curl1);     //返回api的json对象//关闭URL请求if($res1 === FALSE ){echo "CURL Error:".curl_error($curl1);} 解析json  $user_obj=json_decode($res1,true); $data=$user_obj; 

然后我们可以根据openid,进行登录,如果没有账户,让客户绑定手机或者进行注册

<?php
namespace app\api\controller;
header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:*');
// 响应头设置
header('Access-Control-Allow-Headers:*');
header('Access-Control-Allow-Credentials: true');use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Validate;
use think\Db;
use think\Config;
use \app\common\model\User;use app\common\model\UserRule;use app\common\library\Token;
use think\Exception;
use think\Hook;
use think\Request; /*** 会员接口*/
class Weixinlogin extends Api
{public $appid=""; public $secret="";protected $noNeedLogin = ['*'];protected $noNeedRight = '*';public function index(){ if(isset($_GET)&&!empty($_GET)){ $this->weiixnhuidiao();}else{$this->weilogin();    } }public function weilogin(){$appid= $this->appid;$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=";$url.=$appid;$url.="&redirect_uri=";// $url.="http://admin.weixuegu.cn/api/Weixinlogin";$url.="http://".$_SERVER['SERVER_NAME']."/api/Weixinlogin";$url.="&response_type=code";$url.="&scope=snsapi_userinfo";$url.="&state="."123";$url.="#wechat_redirect";header("location: ".$url) ; exit();}public function weiixnhuidiao(){//第二步   if(!isset($_SESSION["openid"]) || empty($_SESSION["openid"])){$appid= $this->appid;$secret= $this->secret; $code = $_GET['code'];$get_token_url='https://api.weixin.qq.com/sns/oauth2/access_token?appid=';$get_token_url.=$appid;$get_token_url.='&secret=';$get_token_url.=$secret;$get_token_url.='&code=';$get_token_url.=$code;$get_token_url.='&grant_type=authorization_code'; $curl = curl_init(); // 启动一个CURL会话curl_setopt($curl, CURLOPT_URL, $get_token_url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);// 跳过证书检查curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $res = curl_exec($curl); if($res === FALSE ){echo "CURL Error:".curl_error($curl);}$json_obj=json_decode($res,true);//     var_dump($json_obj);die;$_SESSION["access_token"]=$json_obj['access_token'];$_SESSION["openid"]=$json_obj['openid'];}//第四步$access_token=$_SESSION['access_token'];$openid=$_SESSION["openid"];$get_user_info_url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';$curl1 = curl_init(); // 启动一个CURL会话curl_setopt($curl1, CURLOPT_URL, $get_user_info_url);curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);// 跳过证书检查curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false); $res1 = curl_exec($curl1);     //返回api的json对象//关闭URL请求if($res1 === FALSE ){echo "CURL Error:".curl_error($curl1);} 解析json  $user_obj=json_decode($res1,true); $data=$user_obj; $url ="  http://swa.weixuegu.cn";$i=0;foreach ($data as $key=>$value) {if($i==0&&!is_array($value)){$i++;$url.="?".$key."=".urldecode($value); }else if(!is_array($value)){$i++;$url.="&".$key."=".urldecode($value);}}echo "   <script>";echo '    window.open("'.$url.'");';echo "  </script>"; echo '  <script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>
<script>// init vConsolevar vConsole = new VConsole();console.log(res);
</script>
';//die;}public function login($openid,$password=""){//username mobile $user = User::get(['weixinopenid' => $openid]);if (!$user) { $this->error("没有账户");}if ($user['status'] == 'hidden') { $this->error("用户被禁用");} if ($user) {$ret = $this->auth->weixinopenid_login($user->username);if ($ret) {$data = ['userinfo' => $this->auth->getUserinfo()];$this->success(__('Logged in successful'), $data);} else {$this->error($this->auth->getError());}}else {$this->error("失败2");}}/*** 设置错误信息** @param $error 错误信息* @return Auth*/public function setError($error){$this->_error = $error;return $this;}public function Binding_user(){//username mobile$mobile = $this->request->request("mobile");$password = $this->request->request("password");$openid = $this->request->request("openid");$headimgurl = $this->request->request("headimgurl");if (!$password ) {$this->error(__('密码不能为空'));}if (!Validate::regex($mobile, "^1\d{10}$")) {$this->error(__('账户格式错误'));}$user = \app\common\model\User::getByMobile($mobile);if (!$user) {// 检测用户名或邮箱、手机号是否存在$this->error(__('你还没有账号哦,请在pc端注册并绑定微信'));}if ($user->password != md5(md5($password) . $user->salt)) {$this->error(__('密码错误'));}$user->weixinopenid = $openid;$user->avatar = $headimgurl;$user->save();// $ret = $this->auth->login($mobile, $password);$this->login($openid);//$this->success(__('Logged in successful'), $ret);}}

微信登录整合——app、小程序、公众号、扫码相关推荐

  1. php企业网站带模块,DouPHP模块化企业网站管理v1.6系统含小程序/公众号源码

    DouPHP是一款轻量级企业网站管理系统,基于PHP+MYSQL架构的, 包含"手机版"."公众号管理模块"."小程序",可以使用它快速搭建 ...

  2. 智慧环卫管理系统方案/APP/小程序/公众号/网站

    目前,环卫日常工作管理及运作上还是按照传统管理模式进行,业务工作建立在工序细分和工作简单化.专业化基础上,相应的组织都是多层次.多部门的"金字塔"型的职能型组织机构.有限的人力资源 ...

  3. VR全景电子商务方案/APP/小程序/公众号/网站

    随着电子商务的发展,电商对产品展示有了更多的尝试:视频体验.flash动画.虚拟空间等方式层出不穷,都是为了加强产品的形象包装与用户体验,最终促进成交率,创造更多效益.而作为用户,最希望能在网站上真实 ...

  4. fiddler使用教程+抓包实践+filder抓包APP+HTTPS,PC微信小程序公众号抓包笔记,fidder插件

    fiddler使用教程+抓包实践+filder抓包APP+HTTPS,PC微信小程序公众号抓包笔记,fidder插件 fiddler使用教程 界面 File->captur traffic 开启 ...

  5. APP、PC客户端抓包、小程序\公众号

    APP.小程序.公众号抓包 一.APP抓包 (一)BurpSuite抓取手机HTTP数据包 1.配置代理IP与端口 2.测试 (二)BurpSuite抓取手机HTTPS数据包 1.安装证书 2.测试 ...

  6. 微信小程序公众号开发

    微信小程序&公众号开发 一.什么是微信开发 二.微信开放平台 三.微信公众平台 四.小程序与公众号的区别 1. 用途不同 2. 运营方式不同 3. 操作方法不同 4. 用户体验不同(公众号操作 ...

  7. 小程序+公众号=App?小程序、公众号、App这三者间如何导流?

    小程序+公众号=App,相信很多做小程序的产品和运营都会一下就看懂这条公式,公众号的内容+小程序的活动/功能可以说是完美的结合体,因此很多产品也将资源大量投入到公众号和小程序.这个公式究竟成立吗?小程 ...

  8. 开源全平台版知识付费系统源码 支持微信小程序+公众号+H5+PC端

    分享一个开源全平台版知识付费系统源码,系统支持微信小程序+公众号+H5+PC端,一套系统实现全端数据及用户体系全面打通,轻松实现店铺全网一站式运营.含完整代码包和详细搭建教程. 系统支持视频课程.音频 ...

  9. 微信html5测试工具,FAutoTest- 微信小程序 / 公众号 H5 自动化利器

    X5 内核 H5 自动化背景 近来有很多童靴咨询如何做微信小程序/公众号等 H5 页面来做自动化,之前写了一篇文章微信小程序自动化测试实践 https://www.cnblogs.com/yyoba/ ...

  10. 微信小程序公众号开发者自动编译,热启动,自动保存

    微信小程序公众号开发者自动编译,热启动,自动保存 1.自动保存 选择左上角设置➡编辑器设置➡勾选需要的设置即可 2.热启动 选择右上角详情➡本地设置➡勾选启动代码自动热重载(建议搭配自动保存使用)

最新文章

  1. 大数据中用到的新的数据类型bigint、decimal、smallint、tinyint
  2. 11.CCNA第十一天-配置OSPF/EIGRP(增强型内部网关协议)
  3. 阿里云安装LNMP以及更改网站文件和MySQL数据目录
  4. 7个建议帮你完成更多的工作
  5. WPF入门教程系列十九——ListView示例(一)
  6. 文献记录(part68)--K- 近邻分类器鲁棒性验证:从约束放松法到随机平滑法
  7. 阿里程序员常用的 15 个高效工具,大部分已开源!
  8. 64位系统使用Access数据库文件的彻底解决方法
  9. java 骗局_Java有陷阱,用时需谨慎——慎用入参做返回值
  10. 面向过程编程与面向对象编程
  11. Android框架揭秘-Zygote笔记
  12. windows关闭休眠
  13. vue-cli 安装 已经preset 配置的删除
  14. 微软MSDN Web cast系列视频教程集锦
  15. mapengpeng1999@163.com 数据库的设计
  16. 花呗上征信,一文看懂征信所有问题
  17. 富途出海淘金:泡沫翻涌 焦虑不止
  18. 程序员新手上路第一步
  19. 《管理学》期末第一次复习
  20. windows8从安装到优化详细全过程——超详细图文教程

热门文章

  1. S7-200 SMART PLC与S7-1500进行S7通信的具体步骤
  2. python的项目骨架_练习 46 - 一个项目骨架 - Learn Python 3 The Hard Way
  3. 12.匹配一次或多次出现的字符
  4. 应届毕业生找Python工作遇到的难题,刚毕业没有工作经验该怎么办?
  5. Android APP杀不死(最大存活方法)
  6. RPA机器人有哪三大优势?
  7. Dreamweaver下拉菜单全攻略
  8. 影像组织方案(BSQ,BIP, BIL )
  9. 招聘帖 | 全国全品类职位列表整理,有需要的加入!
  10. 薛斯通道的十二种买入法和四种卖出法