PHP WeChat Third party implementation of one-click Login and access to user information method (example)

Source: Internet
Author: User
Tags openid
This article mainly introduces the PHP version of the third party to achieve one-click Login and access to user information method, a more detailed analysis of the third-party landing related considerations and implementation skills, the need for friends can refer to the next

Note that to use the login on a third party webpage is required "service number" can be oh, so must go to the official application.

At first you need to enter the public platform to open the development mode, and fill in the Oauth2 callback address, address the domain name of your project can be completed. For example: www.baidu.com or zhidao.baidu.com. If your project is on a level two domain, write a two-level domain name.

The front-end URL authorization address, in the URL, fill in the AppID with the OAuth address in your project's method, as you can see in the code below.

Copy the Code code 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 "> Licensing </a>

Besides the background logic, first call the interface SDK. (There will be later)

Include ('./card/common/class_weixin_adv.php ');

Then fill in the official AppID and secret.

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

Initialize the SDK class, take the code, use the obtained code in the Get out OpenID to 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 get to res from which you can get openid$res= (JSON _decode ($res, True));//convert to array to make it easy to invoke OpenID

Continue calling the SDK method to get the user information. At this time $row has obtained the user information can be var_dump under the key value convenient to deposit the database

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

Getting the user information is done, but that's not enough. What we need is no registration required! So the need to use Openid,openid belongs to the unique credential, Each user has a different OpenID for different public numbers. It can be understood as a user account. I'm using a solution that puts OpenID into a cookie, similar to how users feel about landing, Some key data validation only needs to be compared to the OpenID in the database. Some other ways of using can make people imagine! You can talk to me!

With respect to the authorization of the previous a link, it is also possible to determine whether the cookie has an OpenID, which allows an unauthorized user to jump directly to that address, eliminating the need for a user's one-step operation.

Here is the complete logic code, you can refer to the next!

Public Function Oauth2 () {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 ']) {      //Here Write logic, deposit cookies, database, etc. operation      Cookie (' Weixin ', $row [' OpenID '],25920);    } else{      $this->error (' authorization error, please re-authorize! ');}  } else{    echo "NO CODE";  }  $this->display ();}

SDK code: The official manual, I will not speak more, their own research, very simple!

<?php/** * SDK * pan041ymail@gmail.com */class class_weixin_adv{var $appid = "";  var $appsecret = ""; constructor, get access Token public function __construct ($appid = null, $appsecret = null) {if ($appid) {$this->appi    D = $appid;    } if ($appsecret) {$this->appsecret = $appsecret;    } $this->lasttime = 1395049256; $this->access_token = " Nrzvvpdu7lxcsi7gng2lrucmkbaeczrf0nydbwklng4nmpf88d34pkzdncvhqm4clidlgas18cn1rtsk60p49zizy4ao13sf-eqscs0xjlbad-lkvskk8t7ga    LQ5DIRGXBQQ_TAESSASJJ210VIQTQ "; if (Time () > ($this->lasttime + 7200)) {$url = "Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_crede Ntial&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 (); }}//get user basic information public function Get_user_info ($openid) {$url = "Https://api.weixin.qq.com/cgi-biN/user/info?access_token= ". $this->access_token." &openid= ". $openid."    &AMP;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, Curlopt_returntransfer, 1);    $output = curl_exec ($curl);    Curl_close ($curl);  return $output; }}

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.