php 微信获取code,微信网页授权接口为什么获取不到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。从官网拷贝的链接,不知道咋弄的,少了一个字母...然而这里不需要拼装参数,所以一直没发现...

bVEPJ0?w=780&h=198

回复内容:

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

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

bVEPJ0?w=780&h=198

好巧,昨天晚上也有一个朋友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的步骤

相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值