php - 微信网页授权接口为什么获取不到code(已解决)
迷茫
迷茫 2017-04-10 17:30:57
0
7
487

一.调用第三方接口的方法

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。从官网拷贝的链接,不知道咋弄的,少了一个字母...然而这里不需要拼装参数,所以一直没发现...

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

모든 응답 (7)
Peter_Zhu

好巧,昨天晚上也有一个朋友QQ上问我这问题了。一模一样的问题。

    大家讲道理

    你设置了可信域名吗

      PHPzhong

      你总得写一下你是怎么获取 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;

        }

          Ty80

          要 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的步骤

                최신 다운로드
                더>
                웹 효과
                웹사이트 소스 코드
                웹사이트 자료
                프론트엔드 템플릿
                회사 소개 부인 성명 Sitemap
                PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!