当前位置:Gxlcms > PHP教程 > 如何利用Thinkphp5微信小程序获取用户信息接口

如何利用Thinkphp5微信小程序获取用户信息接口

时间:2021-07-01 10:21:17 帮助过:18人阅读

这篇文章主要介绍了Thinkphp5微信小程序获取用户信息接口的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

Thinkphp5微信小程序获取用户信息接口的实例详解

首先在官网下载示例代码, 选php的,

这里有个坑

官方的php文件,编码是UTF-8+的, 所以要把文件改为UTF-8

然后在Thinkphp5 extend文件夹下建立Wxxcx命名空间,把官方的几个类文件放进去(这里要注意文件夹名, 命名空间名, 类名的, 大小写,一定要一样,官方的文件名和类名大小写不一样)

然后是自己的thinkphp接口代码:

  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leeoo
  5. * Date: 2017/9/14 0014
  6. * Time: 10:43
  7. */
  8. namespace app\api\controller\v1;
  9. use think\Loader;
  10. use think\Request;
  11. use Workerman\Protocols\Http;
  12. use Wxxcx\WXBizDataCrypt;
  13. use first\second\Foo;
  14. class Index
  15. {
  16. public function index($id)
  17. {
  18. return json(['msg' => $id]);
  19. }
  20. public function dologin()
  21. {
  22. $code = Request::instance()->param('code');
  23. $encryptedData = Request::instance()->param('encryptedData');
  24. $iv = Request::instance()->param('iv');
  25. $appid = "你的小程序appid";
  26. $secret = "你的小程序secret";
  27. //appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code
  28. $param = array(
  29. 'appid' => $appid,
  30. 'secret' => $secret,
  31. 'js_code' => $code,
  32. 'grant_type' => 'authorization_code'
  33. );
  34. //http函数为封装的请求函数
  35. $res = http("https://api.weixin.qq.com/sns/jscode2session", $param, 'post');
  36. $arr = json_decode($res, true);
  37. $result = $this->wxdecode($encryptedData, $iv, $arr['session_key'], $appid);
  38. //return json($result);
  39. if ($result) {
  40. return json(['code' => 1]);
  41. } else {
  42. return json(['code' => -1]);
  43. }
  44. }
  45. public function wxdecode($encryptedData, $iv, $sessionKey, $appid)
  46. {
  47. //Loader::import('Wxxcx\WXBizDataCrypt', EXTEND_PATH);
  48. $pc = new WXBizDataCrypt($appid, $sessionKey);
  49. $data = null;
  50. $errCode = $pc->decryptData($encryptedData, $iv, $data);
  51. //echo $data;
  52. //return json(['data'=>$data]);
  53. $data = json_decode($data);
  54. if ($errCode == 0) {
  55. //print($data . "\n");
  56. //dump($data);
  57. return $data;
  58. } else {
  59. //print($errCode . "\n");
  60. //dump($errCode);
  61. return $errCode;
  62. }
  63. }
  64. }

http封装函数:

  1. /**
  2. * 发送HTTP请求方法
  3. * @param string $url 请求URL
  4. * @param array $params 请求参数
  5. * @param string $method 请求方法GET/POST
  6. * @return array $data 响应数据
  7. */
  8. function http($url, $params, $method = 'GET', $header = array(), $multi = false){
  9. $opts = array(
  10. CURLOPT_TIMEOUT => 30,
  11. CURLOPT_RETURNTRANSFER => 1,
  12. CURLOPT_SSL_VERIFYPEER => false,
  13. CURLOPT_SSL_VERIFYHOST => false,
  14. CURLOPT_HTTPHEADER => $header
  15. );
  16. /* 根据请求类型设置特定参数 */
  17. switch(strtoupper($method)){
  18. case 'GET':
  19. $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
  20. break;
  21. case 'POST':
  22. //判断是否传输文件
  23. $params = $multi ? $params : http_build_query($params);
  24. $opts[CURLOPT_URL] = $url;
  25. $opts[CURLOPT_POST] = 1;
  26. $opts[CURLOPT_POSTFIELDS] = $params;
  27. break;
  28. default:
  29. throw new Exception('不支持的请求方式!');
  30. }
  31. /* 初始化并执行curl请求 */
  32. $ch = curl_init();
  33. curl_setopt_array($ch, $opts);
  34. $data = curl_exec($ch);
  35. $error = curl_error($ch);
  36. curl_close($ch);
  37. if($error) throw new Exception('请求发生错误:' . $error);
  38. return $data;
  39. }

然后是小程序的代码:

  1. // 获取用户信息
  2. wx.getSetting({
  3. success: res => {
  4. if (res.authSetting['scope.userInfo']) {
  5. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  6. wx.getUserInfo({
  7. success: res => {
  8. console.log(res);
  9. var encryptedData = res.encryptedData
  10. var iv = res.iv
  11. wx.request({
  12. url: "https://你的服务器地址/dologin",//dologin是访问后端的方法
  13. method: "post",
  14. data: {
  15. code: code,
  16. encryptedData: encryptedData,
  17. iv: iv
  18. },
  19. success: function (ret) {
  20. console.log(ret);
  21. }
  22. })
  23. // 可以将 res 发送给后台解码出 unionId
  24. this.globalData.userInfo = res.userInfo
  25. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  26. // 所以此处加入 callback 以防止这种情况
  27. if (this.userInfoReadyCallback) {
  28. this.userInfoReadyCallback(res)
  29. }
  30. }
  31. })
  32. }
  33. }
  34. })
  35. },

如果有报错, 自己调试一下, 看看哪里的变量有问题 查找原因.

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于thinkPHP框架对接支付宝即时到账接口回调问题的解析

关于thinkPHP微信分享接口JSSDK的用法解析

关于Thinkphp5 微信公众号token验证不成功的原因及解决方法

以上就是如何利用Thinkphp5微信小程序获取用户信息接口的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行