PHP implementation of Web page Authorization development tutorial, PHP Licensing Tutorial
The web authorization is the service number only the Advanced function, the developer can obtain the user's basic information through the authorization, before, wants to obtain the message information only when the user and the public number interacts to obtain the user information according to the OpenID, but the webpage authorization can obtain the user's basic information without needing the message interaction and does not need the attention.
When the Web page is authorized by OAuth2.0, the whole process is divided into three steps:
- User authorization, get code;
- Get Access_token According to code "can get longer validity through Refresh_token refresh"
- Get user information through Access_token and OpenID
A simple encapsulation of the Web page authorization process is made:
<?php/** * Authorization Related interface */class Wechat {//Advanced Function-"developer mode-" Get private $app _id = ' xxx '; Private $app _secret = ' xxxxxxx '; /** * Get Authorization Link * * @param string $redirect _uri jump Address * @param mixed $state parameters */Public Function Get_authorize_ur L ($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 */Public function Get_access_token obtained through Get_authorize_url ($ap p_id = ', $app _secret = ', $code = ') {$token _url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid={$thi s->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code "; $token _data = $this->http ($token _url); if ($token _data[0] = =) {return Json_decode ($token _datA[1], TRUE); } return FALSE; /** * Get Authorized User Information * * @param string $access _token * @param string $open _id */Public function Get_user_inf O ($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] = =) {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_connecttimeout, 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 the whole content of this article, I hope that everyone's study has helped.
Articles you may be interested in:
- PHP Program Authorization validation development Ideas
- User authorization for Sina Weibo API Development profile (PHP basic article)
- PHP uses cookies to control access to authorization methods
- PHP enables PayPal authorized login
- weiphp Public Platform Licensing settings
http://www.bkjia.com/PHPjc/1093698.html www.bkjia.com true http://www.bkjia.com/PHPjc/1093698.html techarticle PHP Implementation of Web page Authorization development tutorial, PHP Licensing Tutorial Web page authorization is the service number of advanced features, developers can obtain the user's basic information after authorization;