php 微信获取code,微信网页授权接口为什么获取不到code(已解决)

  • Post author:
  • Post category:php


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

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

583c0ca6b10abe4014d9d8af3cec990d.png