WeChat login (WeChat PC scan authorization login) Simple php code

Source: Internet
Author: User
Tags openid
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

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.