知识点

相关文章

更多

最近更新

更多

微信会员注册开发【带源码】:网页授权,得到code后在当前页面获取openid,js+php实现跨域请求

2019-03-02 01:08|来源: 网路

开发情景:

 

作者主页:天际app工作室 http://home.zhubajie.com/7145093/

需要引导微信公众平台用户点击链接进入注册页面,在注册页面需要获取用户微信的openid。技术核心是需要借助网页授权,并且在得到授权code时通过js立刻获取openid。

网上关于网页授权后一步步获取openid的文章大多是理论步骤的解说,落实到代码上具体怎么尽可能快的拿到openid的内容很少。笔者十分愤怒,决定写下代码和大家分享

 

这个过程需要一个前端页面代码和一个后端辅助程序,我这里前端是html+js,后端是php。

直接上代码,代码里注释解释的比较清楚:

前端:index.html

 

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd";>  
  2. <!-- 天际app工作室 http://home.zhubajie.com/7145093/ -->  
  3. <html>  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  6. <meta name="viewport" content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">  
  7. <meta name="apple-mobile-web-app-capable" content="yes">  
  8. <meta name="apple-mobile-web-app-status-bar-style" content="black">  
  9. <meta name="format-detection" content="telephone=no">  
  10.   
  11. <title>会员注册</title>  
  12. <script type="text/javascript" src="jquery.js"></script>  
  13. <script type="text/javascript">  
  14.   
  15.  function callback(result) {    
  16.         alert('cucess');    
  17.         alert(result);  //输出openid  
  18.     }    
  19.       
  20. function getQueryString(name) {  
  21.     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
  22.     var r = window.location.search.substr(1).match(reg);  
  23.     if (r != null) return unescape(r[2]); return null;  
  24.     }  
  25.   
  26.   
  27. var code = getQueryString("code");  
  28.   
  29. $.ajax({   
  30.       async: false,   
  31.       url: "http://arvon2012.sinaapp.com/oauth2.php", //这是我的服务端处理文件php的  
  32.       type: "GET",   
  33.       //下面几行是jsoup,如果去掉下面几行的注释,后端对应的返回结果也要去掉注释  
  34.       // dataType: 'jsonp',   
  35.       // jsonp: 'callback', //jsonp的值自定义,如果使用jsoncallback,那么服务器端,要返回一个jsoncallback的值对应的对象.   
  36.       // jsonpCallback:'callback',  
  37.       data: {code:code}, //传递本页面获取的code到后台,以便后台获取openid  
  38.       timeout: 5000,   
  39.       success: function (result) {   
  40.         callback(result);  
  41.       },   
  42.       error: function (jqXHR, textStatus, errorThrown) {   
  43.           alert(textStatus);   
  44.       }   
  45.   });  
  46.   
  47.   
  48. </script>  
  49. </head>  
  50. <body>  
  51. </body>  

 

 

下面是服务端对应的代码,oauth2.php

 

[php]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
    1. <?php  
    2. //天际app工作室 http://home.zhubajie.com/7145093/  
    3. $code = $_GET['code'];//前端传来的code值  
    4.   
    5. $appid = "xxxxxxxxxxxxxxxx";  
    6. $appsecret = "xxxxxxxxxxxxxxxxxxxxxx";  
    7.   
    8. //获取openid  
    9. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";  
    10.   
    11. $result = https_request($url);  
    12.   
    13. $jsoninfo = json_decode($result, true);  
    14. $openid = $jsoninfo["openid"];//从返回json结果中读出openid  
    15.   
    16. $callback=$_GET['callback'];    
    17. // echo $callback."({result:'".$openid."'})";   
    18. echo $openid//把openid 送回前端  
    19.   
    20. function https_request($url,$data = null){  
    21.     $curl = curl_init();  
    22.     curl_setopt($curl, CURLOPT_URL, $url);  
    23.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
    24.     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  
    25.     if (!empty($data)){  
    26.         curl_setopt($curl, CURLOPT_POST, 1);  
    27.         curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
    28.     }  
    29.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    30.     $output = curl_exec($curl);  
    31.     curl_close($curl);  
    32.     return $output;  
    33. }  
    34.   
    35. ?>  

转自:http://www.cnblogs.com/arvon2012/p/3663840

相关问答

更多