微信网页授权demo1
要授权首先要网页域名授权
然后就index.php代码如下
1 2 3 4 5 6 | <?php require_once ( "./function.php" ); $url = 'http://' . $_SERVER [ 'HTTP_HOST' ]. $_SERVER [ 'REQUEST_URI' ]. '?pid=x' ; $getuserinfo = getuserinfo( $url ); print_r( $getuserinfo ); //可以看到获取的用户信息 ?> |
function.php代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function getuserinfo( $url ){ require_once ( "./oauth.php" ); $wechat = new Wechat(); if (isset( $_GET [ 'param' ]) && isset( $_GET [ 'code' ])){ $get = $_GET [ 'param' ]; $code = $_GET [ 'code' ]; if ( $get == 'access_token' && ! empty ( $code )){ $json = $wechat ->get_access_token( $code ); if (! empty ( $json )){ $userinfo = $wechat ->get_user_info( $json [ 'access_token' ], $json [ 'openid' ]); return $userinfo ; } } } else { // $url = 'http://'.$_SERVER['HTTP_HOST'].'/activity/childrenday/index.php?pid=x'; $wechat ->get_authorize_url( $url . '¶m=access_token' , 'STATE' ); } } |
类方法封装在oauth.php里,代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | <?php class Wechat { //高级功能-》开发者模式-》获取 private $app_id = 'wx123456789' ; //服务号appid private $app_secret = '833134#$%^&*($%^&*$%^&*' ; //服务号密匙 /** * 获取微信授权链接 * * @param string $redirect_uri 跳转地址 * @param mixed $state 参数 */ public function get_authorize_url( $redirect_uri = '' , $state = '' ) { $redirect_uri = urlencode( $redirect_uri ); $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect" ; echo "<script language='javascript' type='text/javascript'>" ; echo "window.location.href='$url'" ; echo "</script>" ; } /** * 获取授权token * * @param string $code 通过get_authorize_url获取到的code */ public function get_access_token( $code ) { $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code" ; $token_data = $this ->http( $token_url ); if ( $token_data [0] == 200) { return json_decode( $token_data [1], TRUE); } return FALSE; } /** * 获取授权后的微信用户信息 * * @param string $access_token * @param string $open_id */ public function get_user_info( $access_token = '' , $open_id = '' ) { if ( $access_token && $open_id ) { $info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN" ; $info_data = $this ->http( $info_url ); if ( $info_data [0] == 200) { return json_decode( $info_data [1], TRUE); } } return FALSE; } public function http( $url , $method = 'post' , $postfields = null, $headers = array (), $debug = false) { $ci = curl_init(); /* Curl settings */ curl_setopt( $ci , CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt( $ci , CURLOPT_CONNECTTIMEOUT, 30); curl_setopt( $ci , CURLOPT_TIMEOUT, 30); curl_setopt( $ci , CURLOPT_RETURNTRANSFER, true); switch ( $method ) { case 'POST' : curl_setopt( $ci , CURLOPT_POST, true); if (! empty ( $postfields )) { curl_setopt( $ci , CURLOPT_POSTFIELDS, $postfields ); $this ->postdata = $postfields ; } break ; } curl_setopt( $ci , CURLOPT_URL, $url ); curl_setopt( $ci , CURLOPT_HTTPHEADER, $headers ); curl_setopt( $ci , CURLINFO_HEADER_OUT, true); $response = curl_exec( $ci ); $http_code = curl_getinfo( $ci , CURLINFO_HTTP_CODE); if ( $debug ) { echo "=====post data======\r\n" ; var_dump( $postfields ); echo '=====info=====' . "\r\n" ; print_r(curl_getinfo( $ci )); echo '=====$response=====' . "\r\n" ; print_r( $response ); } curl_close( $ci ); return array ( $http_code , $response ); } } ?> |
搞清楚微信授权流程很重要,看function.php里的这个方法好好体会下。
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· Ollama系列05:Ollama API 使用指南
· 为什么AI教师难以实现