微信登录

阅读本文之前请先了解ThinkSDK的文档

本文基于ThinkSDK,为其补充微信登录demo

增加ThinkSDK的微信第三方登录

1、配置文件增加相应参数

  1. //支付宝登录
  2. 'THINK_SDK_WEIXIN' => array(
  3. 'APP_KEY'    => '', //应用注册成功后分配的 APP ID
  4. 'APP_SECRET' => '', //应用注册成功后分配的KEY
  5. 'CALLBACK'   => URL_CALLBACK . 'weixin',
  6. ),

 

2、WeixinSDK代码

  1. <?php
  2. class WeixinSDK extends ThinkOauth
  3. {
  4. /**
  5. * 获取requestCode的api接口
  6. * @var string
  7. */
  8. protected $GetRequestCodeURL = 'https://open.weixin.qq.com/connect/qrconnect';
  9. /**
  10. * 获取access_token的api接口
  11. * @var string
  12. */
  13. protected $GetAccessTokenURL = 'https://api.weixin.qq.com/sns/oauth2/access_token';
  14. /**
  15. * API根路径
  16. * @var string
  17. */
  18. protected $ApiBase = 'https://api.weixin.qq.com/';
  19. public function getRequestCodeURL()
  20. {
  21. $this->config();
  22. $params = array(
  23. 'appid' => $this->AppKey,          
  24. 'redirect_uri'=>$this->Callback,
  25. 'response_type'=>'code',
  26. 'scope'=>'snsapi_login'
  27. );
  28. return $this->GetRequestCodeURL . '?' . http_build_query($params);
  29. }
  30. /**
  31. * 获取access_token
  32. * @param string $code 上一步请求到的code
  33. */
  34. public function getAccessToken($code, $extend = null){
  35. $this->config();
  36. $params = array(
  37. 'appid'     => $this->AppKey,
  38. 'secret'    => $this->AppSecret,
  39. 'grant_type'    => $this->GrantType,
  40. 'code'          => $code,
  41. );
  42. $data = $this->http($this->GetAccessTokenURL, $params, 'POST');
  43. $this->Token = $this->parseToken($data, $extend);
  44. return $this->Token;
  45. }
  46. /**
  47. * 组装接口调用参数 并调用接口
  48. * @param  string $api    微博API
  49. * @param  string $param  调用API的额外参数
  50. * @param  string $method HTTP请求方法 默认为GET
  51. * @return json
  52. */
  53. public function call($api, $param = '', $method = 'GET', $multi = false){
  54. /* 腾讯微博调用公共参数 */
  55. $params = array(
  56. 'access_token'       => $this->Token['access_token'],
  57. 'openid'             => $this->openid(),
  58. );
  59. $vars = $this->param($params, $param);
  60. $data = $this->http($this->url($api), $vars, $method, array(), $multi);
  61. return json_decode($data, true);
  62. }
  63. /**
  64. * 解析access_token方法请求后的返回值
  65. */
  66. protected function parseToken($result, $extend)
  67. {
  68. $data = json_decode($result,true);
  69. //parse_str($result, $data);
  70. if($data['access_token'] && $data['expires_in']){
  71. $this->Token    = $data;
  72. $data['openid'] = $this->openid();
  73. return $data;
  74. } else
  75. throw new Exception("获取微信 ACCESS_TOKEN 出错:{$result}");
  76. }
  77. /**
  78. * 获取当前授权应用的openid
  79. */
  80. public function openid()
  81. {
  82. $data = $this->Token;
  83. if(!empty($data['openid']))
  84. return $data['openid'];
  85. else
  86. exit('没有获取到微信用户ID!');
  87. }
  88. }
  89. ?>

 

3、获取信息的接口

  1. //登录成功,微信用户信息
  2. public function weixin($token){
  3. $weixin   = ThinkOauth::getInstance('weixin', $token);
  4. $data = $weixin->call('sns/userinfo');
  5. if($data['ret'] == 0){
  6. $userInfo['type'] = 'WEIXIN';
  7. $userInfo['name'] = $data['nickname'];
  8. $userInfo['nick'] = $data['nickname'];
  9. $userInfo['head'] = $data['headimgurl'];
  10. return $userInfo;
  11. } else {
  12. throw_exception("获取微信用户信息失败:{$data['errmsg']}");
  13. }
  14. }
原文地址:https://www.cnblogs.com/smilevv/p/13260969.html