Home > Backend Development > PHP Tutorial > 微信开发,总是获取token失败

微信开发,总是获取token失败

WBOY
Release: 2016-06-20 12:52:56
Original
1838 people have browsed it

$appid = 'wx1aabc8673b8f103a';$appsecret = '9aa99fc9db9bf1321c1afe05feb29b6c';$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx1aabc8673b8f103a&secret=9aa99fc9db9bf1321c1afe05feb29b6c';//echo $access_token;function getDo($url){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$output = curl_exec($ch);curl_close($ch);$jsoninfo = json_decode($output, true);return $jsoninfo;}$arr=getDo($url);$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$arr["access_token"]}&openid={$appid}&lang=zh_CN";        print_r(getDo($info_url));
Copy after login


其实是能获取token,就是在使用是,就失效了,怎么办啊?


回复讨论(解决方案)

微信 有提供官方的php sdk  可以参照其提供的php脚本 来修改你的程序,  

另外  话说你是不是把appid和appsecret暴露了.......

$arr=getDo($url);
 
 
$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$arr["access_token"]}&openid={$appid}&lang=zh_CN";
 
 
        print_r(getDo($info_url));

为什么要调用2次getDo方法呢?

userinfo接口的openid你传了appid,这样传是不对的,openid是用户的唯一标示,改一下吧。

先获取code   然后利用code获取access_token和openid    然后获取用户信息

userinfo只要传token就可以了。返回的是openid之类的,还有,你用了2次getdo,token更新了,而查询的是第一次的token值

openid= {$appid}
传错了。

第一步:用户同意授权,获取code
第二步:通过code换取网页授权access_token

参考: http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

微信 有提供官方的php sdk  可以参照其提供的php脚本 来修改你的程序,  

另外  话说你是不是把appid和appsecret暴露了.......

我不怕,你知道,又
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