<?php $appid = "************"; $secret = "***********"; if (!isset ( $_GET [‘code‘] )) { $back_url = $_GET [‘back_url‘]; if ($back_url) { $back_url_arr = parse_url ( $back_url ); $arr_allow_url = array ( ‘hk.4vtk.com‘, ‘open.weixin.qq.com‘ ); if (in_array ( $back_url_arr [‘host‘], $arr_allow_url )) { Session_start(); $_SESSION[‘back_url‘]=$back_url; $local_url = "http://{$_SERVER [‘HTTP_HOST‘]}{$_SERVER[‘PHP_SELF‘]}"; $local_url=urlencode($local_url); $sit = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$local_url}&response_type=code&scope=snsapi_base&state=1#wechat_redirect"; header ( "Location:" . $sit ); }else{ echo "<h2>未经授权的url</h2>"; exit (); } } else { echo "<h2>回调url错误</h2>"; exit (); } } else { $code = $_GET [‘code‘]; $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_code"; $json = json_decode ( file_get_contents ( $url ), true ); Session_start(); $back_url=$_SESSION[‘back_url‘]; $arr_back_url=parse_url($back_url); if($arr_back_url[‘query‘]){ $return_url = $back_url . "&openid=" . $json [‘openid‘]; }else{ $return_url = $back_url . "?openid=" . $json [‘openid‘]; } header ( "Location:" . $return_url ); } ?>
时间: 2024-10-07 22:27:29