This article mainly shares with you the PHP webpage authorization development tutorial. developers can obtain basic user information after authorization, if you are interested, you can refer to webpage authorization as an advanced function provided by the service number. developers can obtain basic user information after Authorization. Before that, to obtain the message information, you can only obtain the user information based on the openid when the user interacts with the public account. However, webpage authorization does not require message interaction, you do not need to pay attention to the basic information of the user.
Webpage authorization is completed through OAuth2.0. The entire process is divided into three steps:
- User authorization, get code;
- Obtain access_token based on the code. [you can use refresh_token to refresh and obtain a long validity period]
- Obtain user information through access_token and openid
The webpage authorization process is simply encapsulated:
<? Php/*** authorization interface */class Wechat {// advanced functions-developer mode-obtain private $ app_id = 'XXX'; private $ app_secret = 'xxxxxxx '; /*** get authorization link ** @ param string $ redirect_uri jump address * @ param mixed $ state parameter */public function get_authorize_url ($ redirect_uri = '', $ state = '') {$ redirect_uri = urlencode ($ redirect_uri); return" https://open.weixin.qq.com/connect/oauth2/authorize?appid= {$ This-> app_id} & redirect_uri = {$ redirect_uri} & response_type = code & scope = snsapi_userinfo & state = {$ state} # wechat_redirect ";} /*** get authorization token ** @ param string $ code the code obtained through get_authorize_url */public function get_access_token ($ app_id = '', $ app_secret = '', $ code = '') {$ token_url =" https://api.weixin.qq.com/sns/oauth2/access_token?appid= {$ This-> app_id} & secret = {$ this-> app_secret} & code = {$ code} & grant_type = authorization_code "; $ token_data = $ this-> http ($ token_url); if ($ token_data [0] = 200) {return json_decode ($ token_data [1], TRUE );} return FALSE;}/*** obtain authorized user information ** @ param string $ access_token * @ param string $ open_id */public function get_user_info ($ access_token = '', $ open_id = '') {if ($ access_token & $ open_id) {$ info_url =" https://api.weixin.qq.com/sns/userinfo?access_token= {$ Access_token} & openid = {$ open_id} & lang = zh_CN "; $ info_data = $ this-> http ($ info_url ); if ($ info_data [0] = 200) {return json_decode ($ info_data [1], TRUE) ;}return FALSE;} public function http ($ url, $ method, $ postfields = null, $ headers = array (), $ debug = false) {$ ci = curl_init ();/* Curl settings */curl_setopt ($ ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt ($ ci, CURLOPT_CONNECTT IMEOUT, 30); curl_setopt ($ ci, CURLOPT_TIMEOUT, 30); curl_setopt ($ ci, CURLOPT_RETURNTRANSFER, true); switch ($ method) {case 'Post ': curl_setopt ($ ci, CURLOPT_POST, true); if (! Empty ($ postfields) {curl_setopt ($ ci, CURLOPT_POSTFIELDS, $ postfields); $ this-> postdata = $ postfields;} break;} curl_setopt ($ ci, CURLOPT_URL, $ url); curl_setopt ($ ci, CURLOPT_HTTPHEADER, $ headers); curl_setopt ($ ci, CURLINFO_HEADER_OUT, true); $ response = curl_exec ($ ci ); $ http_code = curl_getinfo ($ ci, CURLINFO_HTTP_CODE); if ($ debug) {echo "==== post data =====\ r \ n "; var_dump ($ postfields); echo '==== info ==== '. "\ r \ n"; print_r (curl_getinfo ($ ci); echo '====$ response ==== '. "\ r \ n"; print_r ($ response);} curl_close ($ ci); return array ($ http_code, $ response );}}
The above is all the content of this article, hoping to help you learn.