Micro-trust Web page authorization is the service number of the advanced functions, developers can obtain the user's basic information through authorization, before this, want to get message information only when the user and the public number of interaction based on OpenID to obtain user information, and micro-trust page authorization can be without the need for message interaction, You don't need to be concerned about getting the user's basic information.
Micro-trust Web page when authorized through the OAuth2.0, the whole process is divided into three steps:
- User authorization, access to code;
- Obtain Access_token "through Refresh_token refresh to obtain a longer period of validity" according to code
- Get user information through Access_token and OpenID
A simple encapsulation of the authorization process for micro-credit pages:
<?php/** * Micro-letter Authorization-related interface */class WeChat {/Advanced Function-"developer mode-" Get private $app _id = ' xxx ';
Private $app _secret = ' xxxxxxx '; /** * Access to the micro-letter Authorization Link * * @param string $redirect _uri jump Address * @param mixed $state parameter/Public function Get_auth
Orize_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 "; /** * Obtain Authorization Token * * @param string $code The code/Public function obtained by Get_authorize_url get_access_to Ken ($app _id = ', $app _secret = ', $code = ') {$token _url = ' Https://api.weixin.qq.com/sns/oauth2/access_token?ap
pid={$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 the authorized micro-user Information * * @param string $access _token * @param string $open _id/Public Function ge T_user_info ($access _token = ', $open _id = ') {if ($access _token && $open _id) {$info _url = "http
s://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; The 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 entire content of this article, I hope to help you learn.