Home > Backend Development > PHP Tutorial > Why the WeChat web page authorization interface cannot obtain the code (solved)

Why the WeChat web page authorization interface cannot obtain the code (solved)

WBOY
Release: 2023-03-02 10:56:01
Original
3566 people have browsed it

1. Method of calling third-party interface

<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);
        }
    }    </code>
Copy after login
Copy after login

2. Get the user’s openid

<code>    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);
    }</code>
Copy after login
Copy after login

3. Get the user’s access_token

<code>    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("<hr >");
        var_dump($url);
        var_dump("<hr >");
        dump($res);
    }</code>
Copy after login
Copy after login

The code of the web authorization interface cannot be obtained. The three values ​​printed are as follows:

<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>
Copy after login
Copy after login

The problem has been found. I believe many people like me have not found the problem. As shown below, it should be code. I copied the link from the official website. I don’t know how, but one letter is missing... However, there is no assembly parameter here, so I never found it...
Why the WeChat web page authorization interface cannot obtain the code (solved)

Reply content:

1. Method of calling third-party interface

<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);
        }
    }    </code>
Copy after login
Copy after login

2. Get the user’s openid

<code>    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);
    }</code>
Copy after login
Copy after login

3. Get the user’s access_token

<code>    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("<hr >");
        var_dump($url);
        var_dump("<hr >");
        dump($res);
    }</code>
Copy after login
Copy after login

The code of the web authorization interface cannot be obtained. The three values ​​printed are as follows:

<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>
Copy after login
Copy after login

The problem has been found. I believe many people like me have not found the problem. As shown below, it should be code. The link copied from the official website, I don’t know how, there is one letter missing... However, there is no assembly parameter here, so I never found it...
Why the WeChat web page authorization interface cannot obtain the code (solved)

What a coincidence, a friend asked me this question on QQ last night. Exactly the same problem.

Have you set up a trusted domain name

You have to write down how you obtained the code. . .

In this case, you deliberately write the wrong appid to see if an error will be reported, then you will use the interface that jumps to the first time and deliberately write the wrong appid

In the demo I watched before and the code I wrote, I wrote everything into one method. The method I just wrote for you was split into two methods. Could it be because of this problem? If you can't get the code, you can try writing it together.

For example

public function index(){

<code>if($_GET['code']){
        //第二步
        //第三部
}else{

    //第一步跳转
    //回调地址也是回调到这个方法,获取回调地址,用我下面这个地址来取得当前的地址作为回调,getCurUrl()

}</code>
Copy after login

}

/**

  • php gets the complete url address of the current visit
    */

function getCurUrl() {

<code>$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;</code>
Copy after login

}

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

It is based on oAuth2.0, please refer to the process: wapbaike.baidu.com/item/OAuth2.0/6788617?adapt=1&fr=aladdin

Sorry...
It’s been 2 days and there’s still no result, can you help me

In the third step, why is the code passed as a get parameter? It should be the code obtained by access_token before; the error message is that the code parameter is missing, check the previous steps to obtain the code

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template