微信 在php中 如何获取access_token

网站上调试接口我知道,我现在就想不通过网站,使用PHP代码如何可以获取到用户的access_token
非常感谢

access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。开发者需要进行妥善保存。
access_token的存储至少要保留512个字符空间。access_token的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的
access_token失效。

access_token的获取:

<?php
 
define("APPID", "您的appid");
define("APPSECRET", "您的appsecret ");
 
$token_access_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . APPID . "&secret=" . APPSECRET;
$res = file_get_contents($token_access_url); //获取文件内容或获取网络请求的内容
//echo $res;
$result = json_decode($res, true); //接受一个 JSON æ ¼å¼çš„字符串并且把它转换为 PHP å˜é‡
$access_token = $result['access_token'];
echo $access_token;
 
php>
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-04-10

微信里面都说了通过HTTP协议获取, 用php的file_get_contents()函数就可以了,CURL也行

$rs=file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET');
//返回结果格式{"access_token":"ACCESS_TOKEN","expires_in":7200}
$arr=json_decode($rs,true);
$access_token=$arr['access_token'];

本回答被提问者和网友采纳
第2个回答  2015-04-10
网络抓包呗,还能有什么办法, windows下用wireshark就可以