WeChat public Platform OAUTH20 page authorization PHP Example

Source: Internet
Author: User
Tags openid php example
1, configure the authorization callback page domain name, such as Www.aaa.com

2. Third party Web page of The Sims, fn_system.php

PHP Code

    1. if (emptyempty($_session[' user ']) ){
    2. Header ("location:http://www.aaa.com/uc/fn_wx_login.php");
    3. }Else{
    4. Print_r ($_session[' user ']);
    5. }
    6. ?>

3, when visiting the third party webpage, if there is no session information in the check session, jump to the landing page, fn_wx_login.php

PHP Code

    1. $appid = "The AppID of the public number" ;
    2. $url = ' https://open.weixin.qq.com/connect/oauth2/authorize?appid= ' . $appid . ' &redirect_uri=http%3a%2f%2fwww.aaa.com%2fuc%2ffn_callback.php&resp/span>;
    3. Header ("Location:". ) $url );
    4. ?>

4, in the landing page assembly AppID, bounce URL and other information, and then jump to the user authorization page.

5, on the user authorization page, if the user selected "Consent authorization", then back to the third party Web page to jump back to the address, will be accompanied by the code parameter.

6, the Third party Web page bounce URL, first get code from the request, and then according to code further in exchange for OpenID and Access_token, and then can be based on the OpenID and Access_token invoke the relevant interface to query user information.

PHP Code

  1. $appid = "The AppID of the public number" ;
  2. $secret = "The app secret in the public number" ;
  3. $code = $_get ["code"];
  4. $get _token_url = ' https://api.weixin.qq.com/sns/oauth2/access_token?appid= ' . $appid . ' &secret= ' . $secret . ' &code= ' . $code . ' &grant_type=authorization_code ' ;
  5. $ch = Curl_init ();
  6. curl_setopt ($ch, Curlopt_url,$get _token_url);
  7. curl_setopt ($ch, curlopt_header,0);
  8. curl_setopt ($ch, Curlopt_returntransfer, 1);
  9. curl_setopt ($ch, Curlopt_connecttimeout, ten);
  10. $res = curl_exec ($ch);
  11. Curl_close ($ch);
  12. $json _obj = Json_decode ($res, true);
  13. //Search for user information based on OpenID and Access_token
  14. $access _token = $json _obj [' Access_token '];
  15. $openid = $json _obj [' OpenID '];
  16. $get _user_info_url = ' https://api.weixin.qq.com/sns/userinfo?access_token= ' . $access _token . ' &openid= ' . $openid . ' &LANG=ZH_CN ' ;
  17. $ch = Curl_init ();
  18. curl_setopt ($ch, Curlopt_url,$get _user_info_url);
  19. curl_setopt ($ch, curlopt_header,0);
  20. curl_setopt ($ch, Curlopt_returntransfer, 1);
  21. curl_setopt ($ch, Curlopt_connecttimeout, ten);
  22. $res = curl_exec ($ch);
  23. Curl_close ($ch);
  24. //Parse JSON
  25. $user _obj = Json_decode ($res, true);
  26. $_session [' user '] = $user _obj ;
  27. Print_r ($user _obj);
  28. ?>

The above describes the public platform OAUTH20 Web page authorization PHP example, including the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.