WeChat page Authorization interface Why not get code (resolved)

Source: Internet
Author: User

I. Methods for invoking third-party interfaces

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);        }    }    

Two. Get the user's 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);    }

Three. Get the user's 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);    }

Gets the code for the Web page authorization interface, printing three value results as follows:

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 ]" }

The problem has been found, I believe many people and I did not find the problem. This should be code, as shown below. Copy the link from the official website, do not know how to get, a letter missing ... However, there is no need to assemble parameters, so I have not found ...

Reply content:

I. Methods for invoking third-party interfaces

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);        }    }    

Two. Get the user's 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);    }

Three. Get the user's 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);    }

Gets the code for the Web page authorization interface, printing three value results as follows:

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 ]" }

The problem has been found, I believe many people and I did not find the problem. This should be code, as shown below. Copy the link from the official website, do not know how to get, a letter missing ... However, there is no need to assemble parameters, so I have not found ...

Good coincidence, last night also had a friend QQ asked me this question. The exact same question.

Have you set up a trusted domain name?

You have to write about how you get code ...

That way, you deliberately put appid wrong, see if it will error, on the first jump to the interface, deliberately put AppID write wrong

I looked at the demo and write their own code, I have all the things written into a method, just to you to write that method, I split into two methods, whether it is because of this problem, can not get code, you write together to try.

Say

Public Function index () {

if($_GET['code']){        //第二步        //第三部}else{    //第一步跳转    //回调地址也是回调到这个方法,获取回调地址,用我下面这个地址来取得当前的地址作为回调,getCurUrl()}

}

/**

    • PHP gets the full URL address of the current access
      */

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;

}

to 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, process please refer to: Wapbaike.baidu.com/item/oauth2.0/6788617?adapt=1&fr=aladdin

I'm in trouble.
2 days or no results, can you help?

The third step is why the code when get pass, should be useful before access_token get to the code bar; error content is missing the code parameter, check the previous steps to get code

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.