Micro-trust Web page Authorization (OAuth2.0) PHP source code simple Implementation _php instance

Source: Internet
Author: User
Tags curl openid php source code

feed:  
1. It is recommended to do a study on the OAuth2.0 agreement. &NBSP
2. Micro-credit official documents and micro-trust website tools to be fully utilized. &NBSP
is simpler, directly to the source code. The "xxxxxxxxxx" section is a

that needs to be replaced based on its own environment.

/** * OAuth2.0 Micro-Letter Authorization Login Implementation * * @author Zzy * @ FileName: getwxuserinfo.php//callback Address $url = UrlEncode ("http://www.xxxx
 Xxxxx.com/getwxuserinfo.php ");
 ID of the public number and secret $appid = ' xxxxxxxxx ';
 $appsecret = ' xxxxxxxxx ';

 
 Session_Start (); Gets the code code that is used to request token with the micro-trust server. Note: In accordance with OAuth2.0 requirements, the authorization login here requires a client-side operation if (!isset ($_get[' Code ')) &&!isset ($_session[' code ')) {echo ' <a href= ' https ://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6c11a252ff1d00c4 &redirect_uri= '. $url. ' &response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect "> <font style=" font-size:30 "
  
  > Authorization </font></a> ';
 Exit ///According to the code code to obtain OpenID and Access_token, their own background server directly to the micro-trust server to request if (Isset ($_get[' Code ')) &&!isset ($_session[' token ')
  
  ]) {$_session[' code '] = $_get[' code '];
   $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=". $appid. "&secret=". $appsecret. " &code= ". $_get[' code '."
  &grant_type=authorization_code "; $res = Https_rEquest ($url);
  $res = (Json_decode ($res, true));
 $_session[' token ' = $res;
 
 } print_r ($_session);
 Apply for userinfo information based on the Access_token and OpenID applied to. if (Isset ($_session[' token ' [' Access_token '])) {$url = "https://api.weixin.qq.com/sns/userinfo?access_token=". $_ session[' token ' [' Access_token ']. " &openid= ". $_session[' token ' [' OpenID ']."
  &AMP;LANG=ZH_CN ";
  echo $url;
  $res = Https_request ($url);
  
  $res = Json_decode ($res, true);

 $_session[' userinfo ' = $res;

 } print_r ($_session);
  The Curl function simply encapsulates 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; 
 }

Get the correct results as follows:

Array
(
 [code] => 041gzi4l0tvghg10n75l05fq4l0gzi42
 [token] => Array
  (
   [Access_token] = > Two6w5qmpztzibu3fph2k4edc5bllp4sgeqkc4nbztj-zti-ctzj1srrnl1qgcf2lb1-6o3n7kh2bcxl5bxtqqjegk1cq12l8czf40r9xva
   [expires_in] => 7200
   [Refresh_token] => Iz3olcrkqpbojvssh2bokva09sjvsp1c8ltm7mvxxpfqxsbvi_ WOVMZHJQASZWLMA7TAGGSG3MIJMAHJL7JRJHDQUF1JKBHD6GNDNLTXQ0U
   [OpenID] => ota_xwq4r_5niovmshq
   [scope] = > Snsapi_userinfo
  )

 [userinfo] => Array
  (
   [OpenID] => ota_xwq4r_5niovmshqq
   [ Nickname] => wild fox
   [sex] => 1
   [language] => zh_cn
   [city] => Hangzhou
   [province] => Zhejiang
   [ Country] => China
   [Headimgurl] => http://wx.qlogo.cn/mmopen/ piajxsqbraelwee7rhrt2ibnkc1menu04wiawrw9fkupbbgognrmbynnoeuxicgxoetw5vqqbtrs4fzdxnvawsz6gq/0
   [Privilege] = > Array
    (
    )

  )


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.