在本文中,我们将带你了解微信小程序获取用户手机号记录在这篇文章中,我们将为您详细介绍微信小程序获取用户手机号记录的方方面面,并解答PHP常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的Thi
在本文中,我们将带你了解微信小程序获取用户手机号 记录 在这篇文章中,我们将为您详细介绍微信小程序获取用户手机号 记录 的方方面面,并解答PHP常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的Thinkphp5微信小程序获取用户信息接口的实例详解、【微信小程序】如何获取用户绑定手机号、在微信小程序中如何才可以获取用户手机号信息、如何利用Thinkphp5微信小程序获取用户信息接口。
本文目录一览:- 微信小程序获取用户手机号 记录 (PHP)(微信小程序 获取用户手机号码)
- Thinkphp5微信小程序获取用户信息接口的实例详解
- 【微信小程序】如何获取用户绑定手机号
- 在微信小程序中如何才可以获取用户手机号信息
- 如何利用Thinkphp5微信小程序获取用户信息接口
微信小程序获取用户手机号 记录 (PHP)(微信小程序 获取用户手机号码)
1. 用户登录时需要获取 openid ,同时可以获取 session_key, 二者同时返回, 此时我们要将二者存储在服务端。
2. 小程序端 button 按钮拉起授权, 向api 传递 iv 和 encryptedData 。
3. api 接口 引入 下载的 类文件后, 通过以下两行代码(其中 $sessionKey 是 之前我们在用户登录时存储在服务端的 session_key)
$pc = new \WXBizDataCrypt($APPID, $sessionKey);
$errCode = $pc->decryptData($encryptedData, $iv, $data);
即可通过 $data 拿到 用户手机号。
1
Thinkphp5微信小程序获取用户信息接口的实例详解
ThinkPHP5微信小程序获取用户信息接口的实例详解
首先在官网下载示例代码,选PHP的,
这里有个坑
官方的PHP文件,编码是UTF-8+的,所以要把文件改为UTF-8
然后在ThinkPHP5 extend文件夹下建立Wxxcx命名空间,把官方的几个类文件放进去(这里要注意文件夹名,命名空间名,类名的,大小写,一定要一样,官方的文件名和类名大小写不一样)
然后是自己的thinkPHP接口代码:
use think\Loader;
use think\Request;
use Workerman\Protocols\Http;
use Wxxcx\WXBizDataCrypt;
use first\second\Foo;
class Index
{
public function index($id)
{
return json(['msg' => $id]);
}
public function dologin()
{
$code = Request::instance()->param('code');
$encryptedData = Request::instance()->param('encryptedData');
$iv = Request::instance()->param('iv');
$appid = "你的<a href="https://www.jb51.cc/tag/xiaochengxu/" target="_blank">小程序</a>appid";
$secret = "你的<a href="https://www.jb51.cc/tag/xiaochengxu/" target="_blank">小程序</a>secret";
//appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code
$p<a href="https://www.jb51.cc/tag/ara/" target="_blank">ara</a>m = array(
'appid' => $appid,'secret' => $secret,'js_code' => $code,'grant_type' => 'authorization_code'
);
//http函数为封装的请求函数
$res = http("https://api.weixin.qq.com/sns/jscode2session",$param,'post');
$arr = json_decode($res,true);
$result = $this->wxdecode($encryptedData,$iv,$arr['session_key'],$appid);
//return json($result);
if ($result) {
return json(['code' => 1]);
} else {
return json(['code' => -1]);
}
}
public function wxdecode($encryptedData,$sessionKey,$appid)
{
//Loader::import('Wxxcx\WXBizDataCrypt',EXTEND_PATH);
$pc = new WXBizDataCrypt($appid,$sessionKey);
$data = null;
$errCode = $pc->decryptData($encryptedData,$data);
//echo $data;
//return json(['data'=>$data]);
$data = json_decode($data);
if ($errCode == 0) {
//print($data . "\n");
//dump($data);
return $data;
} else {
//print($errCode . "\n");
//dump($errCode);
return $errCode;
}
}
}
http封装函数:
然后是小程序的代码:
// 可以将 res 发送给<a href="https://www.jb51.cc/tag/houtai/" target="_blank">后台</a>解码出 unionId
this.<a href="https://www.jb51.cc/tag/globalData/" target="_blank">globalData</a>.userInfo = res.userInfo
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
if (this.userInfoReadyCallback) {
this.userInfoReadyCallback(res)
}
}
})
}
}
})
},
如果有报错,自己调试一下,看看哪里的变量有问题 查找原因.
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【微信小程序】如何获取用户绑定手机号
用户调用wx.login()方法,获取登录用户凭证code
wx.login({
success: function(res) {
console.log(''loginCode'', res.code)
}
});
code传给后台,凭证code获取session_key和openid
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
后台会获的用户的openid和session_key
[外链图片转存失败(img-rQSCUZCI-1563290280720)(https://upload-images.jianshu.io/upload_images/2481127-09ea7e9e1f7fecbb.png?imageMogr2/auto-orient/strip|imageView2/2/w/346/format/webp)]
getPhoneNumber组件
拿到encryptedData和iv
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
Page({
getPhoneNumber: function(e) {
console.log(e.detail.errMsg)
console.log(e.detail.iv)
console.log(e.detail.encryptedData)
}
})
将encryptedData和iv传给后台
[外链图片转存失败(img-XB74dpNR-1563290280723)(https://upload-images.jianshu.io/upload_images/2481127-b2d4800dcac8925b.png?imageMogr2/auto-orient/strip|imageView2/2/w/703/format/webp)]
请点赞!因为你们的赞同/鼓励是我写作的最大动力!
欢迎关注达叔小生的简书!
这是一个有质量,有态度的博客
[外链图片转存失败(img-ACAtKBpw-1563290280725)(https://upload-images.jianshu.io/upload_images/11158618-9ab0d3fef85d80ce?imageMogr2/auto-orient/strip|imageView2/2/w/1240)]
本文同步分享在 博客“程序员哆啦A梦”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。
在微信小程序中如何才可以获取用户手机号信息
这篇文章主要为大家详细介绍了微信小程序如何获取用户手机号,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
最近在做一款微信小程序,需要获取用户手机号,具体步骤如下:
流程图:
1、首先,客户端调用wx.login,回调数据了包含jscode,用于获取openid(用户唯一标识)和sessionkey(会话密钥)。
2、拿到jscode后,将其发送给服务端,服务端拿它与微信服务端做交互获取openid和sessionkey。具体获取方法如下:
(1)需要写一个HttpUrlConnection工具类:
public class MyHttpUrlConnection { private final int mTimeout = 10000; // 超时时间 /** * get访问 */ public String[] requestJson(String url) { return request(url); } private String[] request(String connurl) { String[] resultStr = new String[]{"", ""}; StringBuilder resultData = new StringBuilder(""); HttpURLConnection conn = null; try { URL url = new URL(connurl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setUseCaches(false); conn.setConnectTimeout(mTimeout); conn.connect(); int resultCode = conn.getResponseCode(); InputStreamReader in; if (resultCode == 200) { in = new InputStreamReader(conn.getInputStream()); BufferedReader buffer = new BufferedReader(in); String inputLine; while ((inputLine = buffer.readLine()) != null) { resultData.append(inputLine); resultData.append("\n"); } buffer.close(); in.close(); } resultStr[0] = resultData.toString(); resultStr[1] = resultCode + ""; } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } return resultStr; } }
(2)然后通过这个工具类与微信服务器建立连接,获取想要的数据:
String url = "https://api.weixin.qq.com/sns/jscode2session?appid=""&secret=""&js_code=" + jsCode + "&grant_type=authorization_code"; String res[] = connection.requestJson(url); System.out.println(res[0]); JSONObject object = JSON.parseObject(res[0]); String openId = object.getString("openid"); String session_key = object.getString("session_key");
其中appid和secret都是自己开发者账号里可以查询到的,js_code是客户端发过来的,这样在返回的数据中就可以获取sessionkey。
3、服务器A拿到sessionkey后,生成一个随机数我们叫3rdsession,以3rdSessionId为key,以sessionkey + openid为value缓存到redis或memcached中;因为微信团队不建议直接将sessionkey在网络上传输,由开发者自行生成唯一键与sessionkey关联。其作用是: (1)、将3rdSessionId返回给客户端,维护小程序登录态。
(2)、通过3rdSessionId找到用户sessionkey和openid。
4、客户端拿到3rdSessionId后缓存到storage,
5、通过wx.getUserIinfo可以获取到用户敏感数据encryptedData 。
6、客户端将encryptedData、3rdSessionId和偏移量一起发送到服务器A
7、服务器A根据3rdSessionId从缓存中获取session_key
8、在服务器A使用AES解密encryptedData,从而实现用户敏感数据解密。
解密数据需要用到的参数有三个,分别是:
1、encryptedData(密文)
2、iv(向量)
3、aesKey(密钥)也就是sessionkey
在解密的时候要将上述三个变量做Base64解码:
byte[] encrypData = UtilEngine.decode(encData); byte[] ivData = UtilEngine.decode(iv); byte[] sessionKey = UtilEngine.decode(session_key);
然后使用AES解密方法进行解密:
public static byte[] decrypt(byte[] key, byte[] iv, byte[] encData) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); return cipher.doFinal(encData); }
这样在返回的数据中就可以拿到用户的手机号。
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
express搭建查询服务器
使用js自定义trim函数删除两端空格
JavaScript运行原理
以上就是在微信小程序中如何才可以获取用户手机号信息的详细内容,更多请关注php中文网其它相关文章!
如何利用Thinkphp5微信小程序获取用户信息接口
这篇文章主要介绍了thinkphp5微信小程序获取用户信息接口的实例详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
Thinkphp5微信小程序获取用户信息接口的实例详解
首先在官网下载示例代码, 选php的,
这里有个坑
官方的php文件,编码是UTF-8+的, 所以要把文件改为UTF-8
立即学习“PHP免费学习笔记(深入)”;
然后在Thinkphp5 extend文件夹下建立Wxxcx命名空间,把官方的几个类文件放进去(这里要注意文件夹名, 命名空间名, 类名的, 大小写,一定要一样,官方的文件名和类名大小写不一样)
然后是自己的thinkphp接口代码:
<?php /** * Created by PhpStorm. * User: leeoo * Date: 2017/9/14 0014 * Time: 10:43 */ namespace app\api\controller\v1; use think\Loader; use think\Request; use Workerman\Protocols\Http; use Wxxcx\WXBizDataCrypt; use first\second\Foo; class Index { public function index($id) { return json(['msg' => $id]); } public function dologin() { $code = Request::instance()->param('code'); $encryptedData = Request::instance()->param('encryptedData'); $iv = Request::instance()->param('iv'); $appid = "你的小程序appid"; $secret = "你的小程序secret"; //appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code $param = array( 'appid' => $appid, 'secret' => $secret, 'js_code' => $code, 'grant_type' => 'authorization_code' ); //http函数为封装的请求函数 $res = http("https://api.weixin.qq.com/sns/jscode2session", $param, 'post'); $arr = json_decode($res, true); $result = $this->wxdecode($encryptedData, $iv, $arr['session_key'], $appid); //return json($result); if ($result) { return json(['code' => 1]); } else { return json(['code' => -1]); } } public function wxdecode($encryptedData, $iv, $sessionKey, $appid) { //Loader::import('Wxxcx\WXBizDataCrypt', EXTEND_PATH); $pc = new WXBizDataCrypt($appid, $sessionKey); $data = null; $errCode = $pc->decryptData($encryptedData, $iv, $data); //echo $data; //return json(['data'=>$data]); $data = json_decode($data); if ($errCode == 0) { //print($data . "\n"); //dump($data); return $data; } else { //print($errCode . "\n"); //dump($errCode); return $errCode; } } }
http封装函数:
/** * 发送HTTP请求方法 * @param string $url 请求URL * @param array $params 请求参数 * @param string $method 请求方法GET/POST * @return array $data 响应数据 */ function http($url, $params, $method = 'GET', $header = array(), $multi = false){ $opts = array( CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $header ); /* 根据请求类型设置特定参数 */ switch(strtoupper($method)){ case 'GET': $opts[CURLOPT_URL] = $url . '?' . http_build_query($params); break; case 'POST': //判断是否传输文件 $params = $multi ? $params : http_build_query($params); $opts[CURLOPT_URL] = $url; $opts[CURLOPT_POST] = 1; $opts[CURLOPT_POSTFIELDS] = $params; break; default: throw new Exception('不支持的请求方式!'); } /* 初始化并执行curl请求 */ $ch = curl_init(); curl_setopt_array($ch, $opts); $data = curl_exec($ch); $error = curl_error($ch); curl_close($ch); if($error) throw new Exception('请求发生错误:' . $error); return $data; }
然后是小程序的代码:
// 获取用户信息 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 wx.getUserInfo({ success: res => { console.log(res); var encryptedData = res.encryptedData var iv = res.iv wx.request({ url: "https://你的服务器地址/dologin",//dologin是访问后端的方法 method: "post", data: { code: code, encryptedData: encryptedData, iv: iv }, success: function (ret) { console.log(ret); } }) // 可以将 res 发送给后台解码出 unionId this.globalData.userInfo = res.userInfo // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 // 所以此处加入 callback 以防止这种情况 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } }) } } }) },
如果有报错, 自己调试一下, 看看哪里的变量有问题 查找原因.
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
关于thinkPHP框架对接支付宝即时到账接口回调问题的解析
关于thinkPHP微信分享接口JSSDK的用法解析
关于Thinkphp5 微信公众号token验证不成功的原因及解决方法
以上就是如何利用Thinkphp5微信小程序获取用户信息接口的详细内容,更多请关注php中文网其它相关文章!
关于微信小程序获取用户手机号 记录 和PHP的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Thinkphp5微信小程序获取用户信息接口的实例详解、【微信小程序】如何获取用户绑定手机号、在微信小程序中如何才可以获取用户手机号信息、如何利用Thinkphp5微信小程序获取用户信息接口等相关知识的信息别忘了在本站进行查找喔。
本文标签: