WeChat public platform Membership Registration mechanism: page authorization, get code after the current page to get openid,js+php implementation cross-domain request

Source: Internet
Author: User
Tags openid
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;}? -->
  • 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.