How to obtain the user's OpenID on the WeChat public platform webpage

Source: Internet
Author: User
Tags openid
After you click the view type button in the custom menu of WeChat, the WeChat client will open the url value (webpage link) filled in by the developer in the button to open the webpage, however, view cannot obtain the user's openid. You must use the WeChat "webpage authorization to obtain the user's basic information" advanced interface to obtain the user's personal login information.

Method

1. Configure the webpage authorization callback domain name, such as www.111cn.net

2. Simulate the third-party web page of public account, http://www.111cn.net/getcodeurl.php

<? Php
      
If (isset ($ _ SESSION ['user']) {
Print_r ($ _ SESSION ['user']);
Exit;
    }
$ APPID = 'appid of public account on WeChat ';
$ REDIRECT_URI = 'http: // www.111cn.net/callback.php ';
$ Scope = 'snsapi _ base ';
// $ Scope = 'snsapi _ userinfo'; // authorization required
$ Url = 'https: // open.weixin.qq.com/connect/oauth2/authorize? Appid = '. $ APPID. '& redirect_uri = '. urlencode ($ REDIRECT_URI ). '& response_type = code & scope = '. $ scope. '& state = '. $ state. '# wechat_redirect ';
    
Header ("Location:". $ url );
?>

3. In the redirection url of a third-party webpage, first obtain the code from the request, and then exchange openid and access_token based on the code, then, you can call the relevant interfaces of WeChat to query User information based on openid and access_token.

<? Php
// Http://www.111cn.net/callback.php
      
$ Appid = "appid of WeChat official account ";
$ Secret = "app secret of WeChat official account ";
$ Code = $ _ GET ["code"];
$ Get_token_url = 'https: // api.weixin.qq.com/sns/oauth2/access_token? Appid = '. $ appid.' & secret = '. $ secret.' & code = '. $ code.' & grant_type = authorization_code ';

$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ get_token_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10 );
$ Res = curl_exec ($ ch );
Curl_close ($ ch );
$ Json_obj = json_decode ($ res, true );
      
// Query User information based on openid and access_token
$ Access_token = $ json_obj ['Access _ token'];
$ Openid = $ json_obj ['openid'];
$ Get_user_info_url = 'https: // api.weixin.qq.com/sns/userinfo? Access_token = '. $ access_token.' & openid = '. $ openid.' & lang = zh_CN ';
      
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ get_user_info_url );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10 );
$ Res = curl_exec ($ ch );
Curl_close ($ ch );
      
// Parse json
$ User_obj = json_decode ($ res, true );
$ _ SESSION ['user'] = $ user_obj;
Print_r ($ user_obj );
      
?>

References

Webpage authorization to obtain basic user information

Http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

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.