微信网页授权获取不打openid和AccessToken

御宅男小春子
  • 229

    public $appid = ''; //微信APPID,公众平台获取
    public $appsecret = ''; //微信APPSECREC,公众平台获取
    public $index_url = "";//回调地址


    public function getUserInfo()
    {
        $code = $_GET["code"];
        if (empty($code)) {
            $this->authorization_code($this->index_url);
        }
        $access_token = $this->getOauthAccessToken($code);//这个获取不到任何东西 而且也没反应
    }
    
    
    public function authorization_code($redirect_uri)
    {
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid . "&redirect_uri=" . urlencode($redirect_uri) . "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        header("location: $url");
    }
    
        public function getOauthAccessToken($code)
    {

        $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->appid . "&secret=" . $this->appsecret . "&code=" . $code . "&grant_type=authorization_code";
        $access_token_json = $this->https_request($access_token_url);
        $access_token_array = json_decode($access_token_json, true);
        return $access_token_array;
    }
    
    public function https_request($url, $data = null)
    {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    if (!empty($data)) {
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    var_dump($output);
    exit();
    curl_close($curl);
    return $output;
    }
    
    
    
    
    
回复
阅读 2.6k
1 个回答

$access_token_url能够获取到 我把这个链接用在线POST工具 可以获取到openid 我的curl写的有问题吗

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏