Author Development Scenario:
Need to guide the public platform users click the link to enter the registration page, the registration page needs to obtain the user's OpenID. The core of the technology is the need to use web authorization, and in the authorization code to get OpenID immediately through JS.
Online about the authorization of the Web page after the first step to obtain the OpenID is the explanation of the theoretical steps, the implementation of the code specifically how to get the content of OpenID as soon as possible very little. I am very angry, decided to write down the code and share with you
This process requires a front-end page code and a Back-end helper program, where I am html+js and the backend is PHP.
Directly on the code, the comments in the code are explained in more clear:
Front End: index.html
Member Registration
The following is the code for the service side, oauth2.php
!--? php$code = $_get[' code '];//The front end of the code value $appid = "Xxxxxxxxxxxxxxxx"; $appsecret = " Xxxxxxxxxxxxxxxxxxxxxx ";//Get Openid$url =" https://api.weixin.qq.com/sns/oauth2/access_token?appid= $appid &secret= $appsecret &code= $code &grant_type=authorization_code "; $result = Https_request ($url); $jsoninfo = Json_decode ($result , true); $openid = $jsoninfo ["OpenID"];//reads openid$callback=$_get[' callback ' from the return JSON result; echo $callback. " ({result: ' ". $openid."}) "; Echo $openid; Send OpenID back to front-end 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;}? -->