Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn about the simple sample code for PC scan authorization login to php.
Applications are required first. Enterprises, individual industrial and commercial households, and media are required.
Address application https://open.weixin.qq.com/
The simplest php code for me
Start logging in
// ------- Configuration
$ AppID = 'wx033336c794d4 ';
$ AppSecret = 'd4624c36333337af5443d ';
$ Callback = 'HTTP: // www.yun8888.net/weixin/'; // callback address
// Log on
Session_start ();
// ------- Generate a unique random string to prevent CSRF attacks
$ State = md5 (uniqid (rand (), TRUE ));
$ _ SESSION ["wx_state"] = $ state; // save to SESSION
$ Callback = urlencode ($ this-> callback );
$ Wxurl = "https://open.weixin.qq.com/connect/qrconnect? Appid = ". $ this-> AppID." & redirect_uri = {$ callback} & response_type = code & scope = snsapi_login & state = {$ state} # wechat_redirect ";
Header ("Location: $ wxurl ");
Callback address
If ($ _ GET ['state']! = $ _ SESSION ["wx_state"]) {
Exit ("5001 ");
}
$ AppID = 'wx33333333334d4 ';
$ AppSecret = 'd4624c363333330547af5443d ';
$ Url = 'https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = '. $ AppID.' & secret = '. $ AppSecret.' & code = '. $ _ GET ['code'].' & grant_type = authorization_code ';
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
$ Json = curl_exec ($ ch );
Curl_close ($ ch );
$ Arr = json_decode ($ json, 1 );
// Obtain the access_token and openid
Print_r ($ arr );
$ Url = 'https: // api.weixin.qq.com/sns/userinfo? Access_token = '. $ arr ['Access _ token'].' & openid = '. $ arr ['openid'].' & lang = zh_CN ';
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, TRUE );
Curl_setopt ($ ch, CURLOPT_URL, $ url );
$ Json = curl_exec ($ ch );
Curl_close ($ ch );
$ Arr = json_decode ($ json, 1 );
Obtain user information
Print_r ($ arr );
Final output
//----------------------------------------------------------
// Declaration: This code is not perfect. It may not be good. Please speak out!
// Author: xiaozeng
// Deduction: 839024615
// Website: www.yun8888.net
// Communication Group 324098841
// About my ThinkPHP http://www.thinkphp1.cn/u/87696.html
//----------------------------------------------------------
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB