微信網頁授權介面為什麼擷取不到code(已解決)

來源:互聯網
上載者:User

一.調用第三方介面的方法

function https_request($url,$type='get',$res='json',$data = ''){        //1.初始化curl        $curl = curl_init();        //2.設定curl的參數        curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2);        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        if ($type == "post"){            curl_setopt($curl, CURLOPT_POST, 1);            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        }        //3.採集        $output = curl_exec($curl);        //4.關閉        curl_close($curl);        if ($res == 'json') {            return json_decode($output,true);        }    }    

二.擷取使用者的openid

    function getBaseInfo(){        $appid = "我的ID";        $redirect_uri = urlencode("http://www.XXXXXXX.com/XXXXX/XXXXXX.php/Index/getUserOpenId");        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=614#wechat_redirect";        header('location:'.$url);    }

三.擷取使用者的access_token

    function getUserOpenId(){        $appid = "我的ID";        $appsecret = "我的secret";        $code=$_GET['code'];        //2.擷取到網頁授權的access_token        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_cod";        //3.拉取使用者的opendi        $res = $this->https_request($url);        var_dump($code);        var_dump("");        var_dump($url);        var_dump("");        dump($res);    }

擷取不到網頁授權介面的code,列印三個值結果如下:

NULL string(5) "" string(149) "https://api.weixin.qq.com/sns/oauth2/access_token?appid=我的ID &secret=我的secret&code=&grant_type=authorization_cod" string(5) "" array(2) { ["errcode"]=> int(41008) ["errmsg"]=> string(47) "missing code, hints: [ req_id: 3MmDtA0251s113 ]" }

問題已經找到,相信很多人和我一樣沒發現問題在哪。如下所示,應該是code。從官網拷貝的連結,不知道咋弄的,少了一個字母...然而這裡不需要拼裝參數,所以一直沒發現...

回複內容:

一.調用第三方介面的方法

function https_request($url,$type='get',$res='json',$data = ''){        //1.初始化curl        $curl = curl_init();        //2.設定curl的參數        curl_setopt($curl, CURLOPT_URL, $url);        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,2);        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);        if ($type == "post"){            curl_setopt($curl, CURLOPT_POST, 1);            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);        }        //3.採集        $output = curl_exec($curl);        //4.關閉        curl_close($curl);        if ($res == 'json') {            return json_decode($output,true);        }    }    

二.擷取使用者的openid

    function getBaseInfo(){        $appid = "我的ID";        $redirect_uri = urlencode("http://www.XXXXXXX.com/XXXXX/XXXXXX.php/Index/getUserOpenId");        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=614#wechat_redirect";        header('location:'.$url);    }

三.擷取使用者的access_token

    function getUserOpenId(){        $appid = "我的ID";        $appsecret = "我的secret";        $code=$_GET['code'];        //2.擷取到網頁授權的access_token        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_cod";        //3.拉取使用者的opendi        $res = $this->https_request($url);        var_dump($code);        var_dump("");        var_dump($url);        var_dump("");        dump($res);    }

擷取不到網頁授權介面的code,列印三個值結果如下:

NULL string(5) "" string(149) "https://api.weixin.qq.com/sns/oauth2/access_token?appid=我的ID &secret=我的secret&code=&grant_type=authorization_cod" string(5) "" array(2) { ["errcode"]=> int(41008) ["errmsg"]=> string(47) "missing code, hints: [ req_id: 3MmDtA0251s113 ]" }

問題已經找到,相信很多人和我一樣沒發現問題在哪。如下所示,應該是code。從官網拷貝的連結,不知道咋弄的,少了一個字母...然而這裡不需要拼裝參數,所以一直沒發現...

好巧,昨天晚上也有一個朋友QQ上問我這問題了。一模一樣的問題。

你設定了可信網域名稱嗎

你總得寫一下你是怎麼擷取 code的吧。。。

那這樣子,你故意把appid寫錯,看看會不會報錯,就第一次跳轉的那個介面,故意把appid寫錯

我之前看的demo和自己寫的代碼,我都是把所有的東西寫到了一個方法裡面,剛才給你寫的那個方法,我拆成了兩個方法,會不會是因為這個問題,才沒法得到code,你寫到一起試試。

比方說

public function index(){

if($_GET['code']){        //第二步        //第三部}else{    //第一步跳轉    //回調地址也是回調到這個方法,擷取回調地址,用我下面這個地址來取得當前的地址作為回調,getCurUrl()}

}

/**

  • php擷取當前訪問的完整url地址
    */

function getCurUrl() {

$url = 'http://';if (isset($_SERVER ['HTTPS']) && $_SERVER ['HTTPS'] == 'on') {    $url = 'https://';}if ($_SERVER ['SERVER_PORT'] != '80') {    $url .= $_SERVER ['HTTP_HOST'] . ':' . $_SERVER ['SERVER_PORT'] . $_SERVER ['REQUEST_URI'];} else {    $url .= $_SERVER ['HTTP_HOST'] . $_SERVER ['REQUEST_URI'];}// 相容後面的參數組裝if (stripos($url, '?') === false) {    $url .= '?t=' . time();}return $url;

}

要 location.href = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2d6844d53934185f&secret=551ffeb8a5bbd50766bd340141c0963f&code=&grant_type=authorization_cod"

它基於oAuth2.0,流程請參考:wapbaike.baidu.com/item/OAuth2.0/6788617?adapt=1&fr=aladdin

麻煩了...
2天了還是沒結果,能幫下忙嗎

第三步為什麼code時get傳參過來的,之前應該有用access_token擷取到的code吧;報錯內容是缺少code參數,檢查下之前擷取code的步驟

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.