• php后台直接获取code

    function get_code()
    {
       $appid = 'wx2323232322222';
       $url = $this->get_url();

       $redirect_uri = urlencode($url);
       //获取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");
    }
    function get_url()
    {
       //$url = $this->GetCurUrl();
       return 'https://www.b.com';//回调地址
    }
    function GetCurUrl(){
       $url = 'http://';
       if (isset ($_SERVER ['HTTPS']) && $_SERVER ['HTTPS'] == 'on') {
           $url = 'https://';
       }

       $url .= $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'];

       // 兼容后面的参数组装
       if (stripos($url, '?') === false) {
           $url .= '?t=' . time();
       }

       return $url;
    }
    get_url 返回链接在微信里面打开会得到code