微信公众号报redirect_uri参数错误(站点申请http后)

浏览1259

更改get_code方法:

/**
* 获取授权code
*/
function get_code($appid){
   $url = get_url();
   if (strpos($url, '?') === false) {
       $url .= '?getOpenId=1';
   } else {
       $url .= '&getOpenId=1';
   }
   $stulr=str_replace('https://'.SITE_DOMAIN.':443', 'http://'.SITE_DOMAIN, $url);
   $_SESSION['wx_codequan']=1;
   $redirect_uri = urlencode($stulr);
   //获取code后,请求以下链接获取access_token
   $oauth2_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
   header("location: $oauth2_url");
}
更改get_openid方法
/**
* 获取微信返回用户openid
*/
function get_openid($code = ""){
   if (SITE_DOMAIN == 'localhost' || SITE_DOMAIN == '127.0.0.1' || SITE_DOMAIN == 'www.test44.com' || SITE_DOMAIN == '192.168.1.200' || SITE_DOMAIN == '192.168.1.110') {
       return "1";
   }
   if (@$_SESSION["openid"] != "") {
       return @$_SESSION["openid"];
       exit;
   }
   $wx_public = get_public();
   $appid = $wx_public['appid'];
   $appsecret = $wx_public['secret'];
   $code = arg("code", "");
   $getOpenId = arg("getOpenId", "");

   if ($code == "" && $getOpenId == "") {
       get_code($appid);
       exit();
   }
   if(!$_SESSION['wx_codequan']){
       get_code($appid);
       exit();
   }
   $oauth2_code = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $code . "&grant_type=authorization_code";
   $content = https_request($oauth2_code);
   $token = @json_decode($content, true);
   if (empty($token) || !is_array($token) || empty($token['access_token']) || empty($token['openid'])) {
       die(error_msg($token));
   }
   $_SESSION["openid"] = $token['openid'];
   return $token['openid'];
}


  • 暂无任何回答