PHP version of WeChat Third-Party to achieve one-click Login and acquisition of user information, php user information

Source: Internet
Author: User

PHP third-party Method for one-click Login and getting user information, php user information

This example describes how to implement one-click logon and obtain user information by a third party in PHP. We will share this with you for your reference. The details are as follows:

Note: to log on to a third-party webpage, you need a "Service Number". Therefore, you must go to the official website to apply.

In the beginning, you need to enter the public platform to enable the development mode, and fill in the oau2's callback address, and enter your project's domain name for the address. for example: www.baidu.com or zhidao.baidu.com. if your project is in the second-level domain name, write the second-level domain name

The authorization address of the front-end url. In the url, enter the address of appid and oauth in the method in your project. For details, see the following code.
Copy codeThe Code is as follows: <a href = "https://open.weixin.qq.com/connect/oauth2/authorize? Appid = appid & redirect_uri = http://www.xxxxxx.com/action/function/oauth2&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect "> authorization </a>

Besides the background logic, call the SDK of the interface first (which will be available later)

include('./Card/Common/class_weixin_adv.php');

Enter the official appid and secret.

$weixin=new class_weixin_adv("appid", "secret");

Initialize the SDK class, get the code, and use the obtained code to get the openid. See the following code comment!

$ Url = "https://api.weixin.qq.com/sns/oauth2/access_token? Appid = appid & secret = secret & code = ". $ _ GET ['code']. "& grant_type = authorization_code"; $ res = $ weixin-> https_request ($ url ); // call the SDK method to obtain res. You can obtain openid $ res = (json_decode ($ res, true). // convert it to an array to call openid.

Continue to call the SDK method to obtain user information. Now $ row has obtained user information. You can run the var_dump command to check whether the key value is saved to the database.

$row=$weixin->get_user_info($res['openid']);

It is done to get user information, but this is not enough. What we need is no registration! Therefore, you need to use openid, which is a unique credential. each user has a different openid for different public accounts. it can be understood as a user account. I am using the solution to store openid into cookies. Similar to the user's login feeling, some key data verification only needs to be compared with the openid in the database. other methods can be used to give full play to your imagination! You can leave a message to me!

With regard to the authorization of the previous a link, you can also determine whether the cookie exists in openid, so that unauthorized users can directly jump to the address, saving users' step-by-step operations.

The following is the complete logic Code. For more information, see!

Public function oau2() {include ('. /Card/Common/class_weixin_adv.php '); $ weixin = new class_weixin_adv ("appid", "secret"); if (isset ($ _ GET ['code']) {$ url = "https://api.weixin.qq.com/sns/oauth2/access_token? Appid = appid & secret = secret & code = ". $ _ GET ['code']. "& grant_type = authorization_code"; $ res = $ weixin-> https_request ($ url); $ res = (json_decode ($ res, true )); $ row = $ weixin-> get_user_info ($ res ['openid']); if ($ row ['openid']) {// write the logic here and store the cookie, database and other operation cookies ('weixin', $ row ['openid'], 25920);} else {$ this-> error ('authorization error, Please authorize again! ') ;}} Else {echo "no code" ;}$ this-> display ();}

SDK code: there is an official manual. I will not talk about it more. I will study it myself. It is very simple!

<? Php/*** SDK * pan041ymail@gmail.com */class class_weixin_adv {var $ appid = ""; var $ appsecret = ""; // constructor, obtain Access Token public function _ construct ($ appid = NULL, $ appsecret = NULL) {if ($ appid) {$ this-> appid = $ appid ;} if ($ appsecret) {$ this-> appsecret = $ appsecret;} $ this-> lasttime = 1395049256; $ this-> access_token = "token Duration "; if (time ()> ($ this-> lasttime + 7200) {$ url =" https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = ". $ this-> appid. "& secret = ". $ this-> appsecret; $ res = $ this-> https_request ($ url); $ result = json_decode ($ res, true ); $ this-> access_token = $ result ["access_token"]; $ this-> lasttime = time () ;}// obtain basic user information public function get_user_info ($ openid) {$ url = "https://api.weixin.qq.com/cgi-bin/user/info? Access_token = ". $ this-> access_token. "& openid = ". $ openid. "& lang = zh_CN"; $ res = $ this-> https_request ($ url); return json_decode ($ res, true );} // https request public function https_request ($ url, $ data = null) {$ curl = curl_init (); curl_setopt ($ curl, CURLOPT_URL, $ url); curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (! Empty ($ data) {curl_setopt ($ curl, CURLOPT_POST, 1); curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data);} curl_setopt ($ curl, expires, 1 ); $ output = curl_exec ($ curl); curl_close ($ curl); return $ output ;}}

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.