Nowadays, WeChat has become a must-have social tool for everyone. If you can log in with WeChat on the membership platform, it will greatly increase the bonding between users and the platform. This time, I will lead you to implement authorized login on mobile WeChat.
1. WeChat login on the web page must be implemented in WeChat.
Different from the PC-side scan code login, the mobile web page cannot be logged in with WeChat authorization using the ordinary browser, and must be used in WeChat. This must be clear.
2. Preparation work.
We need to have a certified WeChat service account, which is a necessary condition. We open the login WeChat public platform and find "Interface Permissions".
#We already have the permission to obtain web user information. If the service account is not authenticated, this permission is not available.
Then click Modify to configure the domain name that requires WeChat authorization. Note that authorization needs to be written as www.abc.com, and http cannot be included. The wrong way of writing is: http://www.abc.com.
3. Authorize WeChat to obtain WeChat user information
After the preparations are completed, we can follow the documenthttps://developers.weixin.qq. com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html to proceed step by step.
Step one: The user agrees to authorize and obtain the code.
public function wechat_login(){ $appid='wxff5b68b041a4fb11';//公众号基本配置中获取 $redirect_uri='http://a.xx.cn/mobile/user/weixin_m.html';//用户授权后跳转处理的地址 $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; return redirect($url); }
We guide users to access the wechat_login method. Two parameters need to be prepared here. The appid is obtained in the basic configuration of the official account. The redirect_uri is the processing address callback after the WeChat user authorizes it. It must be written here. http absolute address, an address that can be accessed from the external network.
Second step: Exchange web page authorization access_token through code
We establish the callback method weixin_m in the first step. We first print the code to check whether we can obtain it. This is also a common step in program debugging. If there is no problem with this step, proceed to the next step.
public function weixin_m(){ $code=input('get.code'); halt($code); }
Next we get the access_token.
public function weixin_m(){ $code=input('get.code'); $appid='wxff5b68b241a4fb11';//公众号基本配置中获取 $appsecret='412a24b17e61317d589b8bf92f374ffc'; $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code"; $res=json_decode(file_get_contents($url),true);//json转数组 }
We go to the request address to obtain the access_token, which requires three parameters: appid, appsecret, code. The appsecret is also obtained in the basic configuration of the official account.
After obtaining, WeChat returns json format by default, so we convert json into an array, so that it is convenient for operation.
The print array is as follows:
array:6 [ "access_token" => "37_3RRxoQZKuECSpCfGMYLcO-1ZXu_uhTkdku_m29u4XfSq9-Ve_0Fn5_K6vUBpkiL1iXRpEBepOfeMZZA7TGm-bg" "expires_in" => 7200 "refresh_token" => "37_CjmxjzqBCqiIVH3aKjR22RQniCr_7DYYJYgodMONV5822FnfKuq0VwOS0B9dfucHf6GxTjXbczruwS5NIkGAWw" "openid" => "oaq-51XAHNaj9qUxVwYu3-elVTa0" "scope" => "snsapi_userinfo" "unionid" => "oO0Bhv6ZSw4ZYV60CMzi2p4eUO7s" ]
In this way, we successfully obtained access_token and openid.
Step 3: Pull user information (need to have scope snsapi_userinfo)
We still follow the document to request the address:
$user_url="https://api.weixin.qq.com/sns/userinfo?access_token=".$res['access_token']."&openid=".$res['openid']."&lang=zh_CN"; $userData=json_decode(file_get_contents($user_url),true);//json转数组 halt($userData);
The request needs to carry the access_token and openid parameters we obtained in the second step. The return from WeChat is still in json format, and we convert json into an array again.
Print the array as follows:
After obtaining the user data, we can store the array or perform other operations.
Up to this point, "Mobile WeChat Authorized Login" has been introduced. The overall difficulty is not very difficult. It also requires everyone to read the document patiently. The document is the best teacher. Come on, everybody!
The above is the detailed content of TP's mobile WeChat authorized login. For more information, please follow other related articles on the PHP Chinese website!