WeChat web page authorization (OAuth2.0) PHP source code is simple implementation, oauth2.0 source code

Source: Internet
Author: User
Tags openid

Web page authorization (OAuth2.0) PHP source code is simple implementation, oauth2.0 source code

Abstract:

1. We recommend that you learn the OAuth2.0 protocol.

2. Official documents and tools should be fully utilized.

 

It is relatively simple. You can directly post the source code. The "xxxxxxxxxx" part must be replaced based on the environment.

/*** OAuth2.0 authorized login implementation ** @ author login * @ file name: GetWxUserInfo. php * // callback address $ url = urlencode ("http://www.xxxxxxxxx.com/GetWxUserInfo.php"); // id and secret of the Public Account $ appid = 'xxxxxxxxxx'; $ appsecret = 'xxxxxxxxxxxx '; session_start (); // get the code, used to apply for a token with the server. Note: According to OAuth2.0 requirements, authorized logon requires user-side operations if (! Isset ($ _ GET ['code']) &! Isset ($ _ SESSION ['code']) {echo '<a href = "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = wx6c11a252ff1d00c4 & redirect_uri = '. $ url. '& response_type = code & scope = snsapi_userinfo & state = 1 # wechat_redirect "> <font>; exit ;}// obtain the openid and access_token Based on the code, if (isset ($ _ GET ['code']) &! Isset ($ _ SESSION ['Token']) {$ _ SESSION ['code'] = $ _ GET ['code']; $ url = "https://api.weixin.qq.com/sns/oauth2/access_token? Appid = ". $ appid. "& secret = ". $ appsecret. "& code = ". $ _ GET ['code']. "& grant_type = authorization_code"; $ res = https_request ($ url); $ res = (json_decode ($ res, true )); $ _ SESSION ['Token'] = $ res;} print_r ($ _ SESSION); // apply for Userinfo Based on the requested access_token and openid. If (isset ($ _ SESSION ['Token'] ['Access _ token']) {$ url = "https://api.weixin.qq.com/sns/userinfo? Access_token = ". $ _ SESSION ['Token'] ['Access _ token']. "& openid = ". $ _ SESSION ['Token'] ['openid']. "& lang = zh_CN"; echo $ url; $ res = https_request ($ url); $ res = json_decode ($ res, true ); $ _ SESSION ['userinfo'] = $ res;} print_r ($ _ SESSION); // The cURL function encapsulates function https_request ($ url, $ data = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE); cur Rochelle setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} curl_setopt ($ curl, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output ;}

 

The correct result is as follows:

 

Array ([code] => token [token] => Array ([access_token] => TWo6w5QMpzTZibu3FPh2k4EdC5bllp4sGeQkC4NbZtj-zti-ctZj1SrrNL1qGCf2lB1-6o3N7kh2bcxl5bxtQqJEGk1cq12l8CzF40R9XvA [expires_in] => 7200 [refresh_token] => token [openid] => ota_XwQ4r_5nioVmshQ [scope] => snsapi_userinfo) [userinfo] => Array ([openid] => ota_XwQ4r_5nioVmshQq [nickname] => yehu [sex] => 1 [language] => zh_CN [city] => Hangzhou [province] => Zhejiang [country] => China [headimgurl] => http://wx.qlogo.cn/mmopen/PiajxSqBRaELwee7rhrt2ibnkC1MEnu04WiaWrw9FkuPBbGOgnrMbynNoEuxicgXOetW5VqQbTrS4fZDXNvAWsz6GQ/0 [privilege] => Array ()))

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.