WeChat public platform OAuth20 web page authorization php example

WBOY
Release: 2016-07-28 08:27:40
Original
1383 people have browsed it

1. Configure the domain name of the authorization callback page, such as www.aaa.com

2. Simulate the third-party webpage of the official account, fn_system.php

Php code 微信公众平台OAuth20网页授权php示例

  1. if(emptyempty($_SESSION['user'])) {
  2. header ("Location:http://www.aaa.com/uc/fn_wx_login.php"); [
  3. 'user']);
  4. }
  5. ?>
  6. 3. When accessing a third-party web page, if there is no session information in the session , then jump Go to the login page, fn_wx_login.phpPhp code
  7. =  微信公众平台OAuth20网页授权php示例

    "Appid of official account in WeChat"

      ;
    1. $url
    2. =
    3. 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='
    4. .$appid. '&redirect_uri=http%3a%2f%2fwww.aaa.com%2fuc%2ffn_callback.php&resp/span>;
    5. header("Location:".$url); ?>
    6. 5. On the user authorization page of WeChat, if the user selects "Agree Authorization", the code parameter will be attached when WeChat jumps back to the bounce address of the third-party web page.
    7. 6. In the bounce URL of the third-party web page, first obtain the code from the request, and then exchange it for openid and access_token based on the code. Then you can call the relevant interface of WeChat based on openid and access_token to query user information. Php code
      1. $appid = "appid of the official account in WeChat";
      2. $secret = " The official account is in the WeChat app secret"; $get_token_url
      3. = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret
      4. . '&code='.$code.'&grant_type=authorization_code'; curl_init(); curl_setopt ($ch,CURLOPT_URL,$get_token_url); curl_setopt($ch,CURLOPT_HEADER,0); curl_setopt($ch
      5. , CURLOPT_RETURNTRANSFER, 1);
      6. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
      7. $res = curl_exec($ch );
      8. curl_close( $ch); //Query user information based on openid and access_token
      9. $access_token
      10. = $json_obj['access_token'
      11. ]; $openid = $json_obj
      12. ['openid' ];
      13. $get_user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token
      14. . ' &openid='
      15. .$openid.'&lang=zh_CN'
      16. ; $ch = curl_init ();
      17. curl_setopt( $ch,CURLOPT_URL,$get_user_info_url); curl_setopt($ch
      18. ,CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ) ; curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $res = curl_exec($ch );
      19. curl_close($ch
      20. );
      21. // Parse json $res,true);
      22. $ _SESSION['user'] =
      23. $user_obj;
      24. print_r(
      25. $user_obj);
      26. ?> The above has introduced the WeChat public platform OAuth20 web page authorization PHP example, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!