当前位置:   article > 正文

thinkPHP小程序获取openid unionid_thinkphp3.2 获取unionid

thinkphp3.2 获取unionid

  1. function getWxinfo()
  2. {
  3. $arr['appid'] = config('socket.appid');
  4. $arr['secret'] =config('socket.secret');
  5. $arr['grant_type'] ='authorization_code';
  6. $arr['js_code'] = input('param.code');
  7. $encryptedData = input('param.encryptedData');
  8. $iv = input('param.iv');
  9. $url = "https://api.weixin.qq.com/sns/jscode2session?";
  10. $url = $url . http_build_query($arr);
  11. $http = new HttpClient();//添加账户记录
  12. $res = $http->get($url);
  13. $res=json_decode($res,TRUE);
  14. // var_dump($res);
  15. $sessionKey=$res['session_key'];
  16. /* var_dump($pc); */
  17. $errCode = $this->decryptData($arr['appid'], $sessionKey,$encryptedData, $iv);
  18. if ($errCode != -41001 &&$errCode != -41002&&$errCode != -41003&&$errCode != -41004) {
  19. return $errCode;
  20. } else {
  21. throw new \Exception($errCode);
  22. }
  23. }
  24. /**
  25. * 微信信息解密
  26. * @param string $appid 小程序id
  27. * @param string $sessionKey 小程序密钥
  28. * @param string $encryptedData 在小程序中获取的encryptedData
  29. * @param string $iv 在小程序中获取的iv
  30. * @return array 解密后的数组
  31. */
  32. function decryptData($appid , $sessionKey, $encryptedData, $iv){
  33. $OK = 0;
  34. $IllegalAesKey = -41001;
  35. $IllegalIv = -41002;
  36. $IllegalBuffer = -41003;
  37. $DecodeBase64Error = -41004;
  38. if (strlen($sessionKey) != 24) {
  39. return $IllegalAesKey;
  40. }
  41. $aesKey=base64_decode($sessionKey);
  42. if (strlen($iv) != 24) {
  43. return $IllegalIv;
  44. }
  45. $aesIV=base64_decode($iv);
  46. $aesCipher=base64_decode($encryptedData);
  47. $result=openssl_decrypt( $aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
  48. $dataObj=json_decode( $result );
  49. if( $dataObj == NULL )
  50. {
  51. return $IllegalBuffer;
  52. }
  53. if( $dataObj->watermark->appid != $appid )
  54. {
  55. return $DecodeBase64Error;
  56. }
  57. $data = json_decode($result,true);
  58. return $data;
  59. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/525063
推荐阅读
相关标签
  

闽ICP备14008679号