WeChat authorization and verification

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about the auth authorization verification class, which has been modified on the basis of others. For useful reference, refer

/**
* Authorization related interfaces
*
*
*/

Class wechatauth {

// Advanced functions-developer mode-Get
Private $ app_id = 'wxa0c4 *************';
Private $ app_secret = 'e095b6dc **********************';

/**
* 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 the 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;
}

/**
* Verify authorization
*
* @ Param string $ access_token
* @ Param string $ open_id
*/
Public function check_access_token ($ access_token = '', $ open_id = '')
{
If ($ access_token & $ open_id)
{
$ Info_url = "https://api.weixin.qq.com/sns/auth? 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;
}


// Curl
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 );
}

}
Release the output demo. For more information, see // Authentication Link
Public function wxurl (){
Import ('myclass. Wechatauth ', APP_PATH );
$ Wechat = new wechatauth ();
$ Token = session ('Token'); // check whether authorization is enabled.
If (! Empty ($ token )){
Print_r ($ token );
$ State = $ Wechat-> check_access_token ($ token ['Access _ token'], $ token ['openid']); // check whether the token is available (successful information: "errcode": 0, "errmsg": "OK ")
Print_r ($ state );
}
$ Url = $ Wechat-> get_authorize_url ('HTTP: // twx.vjiankang.org/wsite/test/wxrurl', '1'); // After the url is authorized, it will be processed below
Echo 'authorization ';

}

// Return a string
Public function wxrurl (){
Import ('myclass. Wechatauth ', APP_PATH );
$ Wechat = new wechatauth ();
Print_r ($ _ GET); // information obtained on this page after successful authorization

$ Token = $ Wechat-> get_access_token ('','', $ _ GET ['code']); // after the authorization is confirmed, the token is obtained based on the returned code.
Print_r ($ token );
Session ('Token', $ token); // Save the authorization information

$ User_info = $ Wechat-> get_user_info ($ token ['Access _ token'], $ token ['openid']); // get user information
Print_r ($ user_info );

}
It should be noted that the url we use to return must be changed to the same domain name in the public platform-> developer-> auth Authentication

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.