This article mainly introduces the THINKPHP5 small program to obtain the user information interface examples of relevant information, hope that through this article can help everyone, the need for friends can refer to the next
THINKPHP5 Small program to get the user information interface instance detailed
First download the sample code on the official website, choose PHP,
There's a hole in here.
The official PHP file, the encoding is utf-8+, so you want to change the file to UTF-8
Then in the THINKPHP5 extend folder under the establishment of WXXCX namespace, the official several classes of files (note the folder name, namespace name, class name, case, must be the same, the official file name and the class name is not the same case)
Then is your own thinkphp interface code:
<?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 = "Your little program AppID"; $secret = "Your little program secret"; appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code $param = Array (' app ID ' = $appid, ' secret ' + $secret, ' js_code ' + $code, ' grant_type ' = ' authorization_code ' ); The HTTP function is for the encapsulated request function $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\wxbizdatacryp T ', 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 wrapper function:
/** * Send HTTP request method * @param string $url request URL * @param array $params request parameter * @param string $method request method Get/post * @retu RN Array $data response data */function http ($url, $params, $method = ' GET ', $header = Array (), $multi = False) {$opts = array (curlopt_timeout = Curlopt_returntransfer = 1, Curlopt_ssl_verifypeer = False, curlopt _ssl_verifyhost = False, Curlopt_httpheader = $header); /* Set specific parameters according to the request type */switch (Strtoupper ($method)) {case ' GET ': $opts [curlopt_url] = $url. '?' . Http_build_query ($params); Break Case ' POST '://Determine if file transfer $params = $multi? $params: Http_build_query ($params); $opts [Curlopt_url] = $url; $opts [Curlopt_post] = 1; $opts [Curlopt_postfields] = $params; Break Default:throw New Exception (' Unsupported request mode! '); }/* Initializes and performs a curl request */$ch = Curl_init (); Curl_setopt_array ($ch, $opts); $data = curl_exec ($ch); $error = Curl_error ($ch); Curl_closE ($ch); if ($error) throw new Exception (' request error: '. $error); return $data; }
Then the code of the applet:
Get user Information wx.getsetting ({ Success:res + = { if (res.authsetting[' Scope.userinfo ')} { //has been authorized, You can call GetUserInfo directly to get the avatar nickname, not the frame wx.getuserinfo ({ success:res = { Console.log (res); var EncryptedData = Res.encrypteddata var IV = RES.IV wx.request ({ URL: "https://Your server address/dologin",// Dologin is the method of accessing the backend : "POST", data: { code:code, encrypteddata:encrypteddata, iv:iv }, success:function (ret) { console.log (ret); } }) Res can be sent to the background decoded out Unionid this.globalData.userInfo = res.userinfo //Because GetUserInfo is a network request, may be after page.onload To return //So add callback here to prevent this situation if (this.userinforeadycallback) { This.userinforeadycallback (res) } } }) } } }) },
If there is an error, debug yourself and see where the variable has a problem to find the cause.
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!