PHP Micro-letter Third party implementation of one-click Login and access to user information methods _php tips

Source: Internet
Author: User
Tags curl openid

This article describes the PHP version of micro-letter third party to achieve a key login and access to user information methods. Share to everyone for your reference, specific as follows:

Note that the use of micro-letters in Third-party Web login is required "service number" can oh, so must go to the official application.

First you need to enter the micro-credit public platform to open the development model, and fill in the Oauth2 callback address, address to fill in the domain name of your project. For example: www.baidu.com or zhidao.baidu.com. If your project is in level two domain name write two domain name

Front-End URL authorization address, in the URL to fill out AppID and your project in the method of the OAuth address, specific in the following code can be seen.

Copy 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 "> Authorization </a>

Again, background logic, first call the micro-interface SDK. (There will be later)

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

After that, the official AppID and secret

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

Initialize the SDK class, take the code, and use the acquired codes to get an OpenID look at the comments below!

$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);//Invoke the SDK method to get the OpenID
$res = (Json_decode ($res, True) from the res;//Convert to Array Easy Call OpenID

Continue invoking the SDK method to obtain user information. At this point $row has obtained user information can be var_dump to check the value of the key to facilitate the database

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

Getting user information is a success, but it's not enough. What we need is no registration! So you need to use Openid,openid to belong to the unique voucher, Each user has different OpenID for different public numbers. Can be understood as a user account of the feeling. I'm here with a solution that stores OpenID in cookies, similar to the sense of user login, Some key data validation only needs to be compared to the OpenID in the database. Other use of some of the methods can give play to everyone's imagination! You can communicate with me!

On the previous a link authorization, you can also determine whether the cookie exists OpenID, so that unauthorized users to jump directly to the address, save the user's one-step operation.

Here is the complete logic code, we 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 is written logically, stored in cookies, database, etc. operation
      cookies (' Weixin ', $row [' OpenID '],25920);
    } else{
      $this->error (' authorization error, re-authorize! ');
    }
  else{
    echo "NO CODE";
  }
  $this->display ();
}

SDK code: Micro-letter Official has a handbook, I will not say more, their own research, very simple!

<?php/** * Micro-Letter SDK * pan041ymail@gmail.com * * class Class_weixin_adv {var $appid = "";
  var $appsecret = ""; constructor to get 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 = " Nrzvvpdu7lxcsi7gng2lrucmkbaeczrf0nydbwklng4nmpf88d34pkzdncvhqm4clidlgas18cn1rtsk60p49zizy4ao13sf-eqscs0xjlbad-lkvskk8t7ga
    LQ5DIRGXBQQ_TAESSASJJ210VIQTQ "; if (Time () > ($this->lasttime + 7200)) {$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_cred Ential&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;

 }
}

For more information on PHP related content readers can view the site topics: "PHP micro-credit Development Skills summary", "PHP coding and transcoding Operation skills Summary", "PHP Network Programming Skills Summary", "PHP basic Grammar Introductory Course", "PHP string (String) Usage Summary", "php+ MySQL Database operations Introduction tutorial and PHP Common database operation Skills Summary

I hope this article will help you with the PHP program design.

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.