access_token 是公众号的全局惟一接口调用凭据,公众号调用各接口时都需使用 access_token。
access_token 的存储至少要保留 512 个字符空间。access_token 的有效期目前为 2 个小时。php
https 请求方式:GET
https://api.weixin.qq.com/cgi...
参数 | 是否必须 | 说明 |
---|---|---|
grant_type | Y | 获取access_token填写client_credential |
appid | Y | 第三方用户惟一凭证 |
secret | Y | 第三方用户惟一凭证密钥,即 appsecret |
<?php $appid = ""; $appsecret = ""; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; /* * curl_init() 为 PHP 函数 * curl_setopt 设置 cURL 的传输选项 **/ $ch = curl_init(); // 建立一个 cURL 资源 curl_setopt($ch, CURLOPT_URL, $url); // CURLOPT_URL 目标 url 地址 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // CURLOPT_SSL_VERIFYPEER False: 终止 cURL 在服务器进行验证 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // CURLOPT_RETURNTRANSFER 返回原生的(Raw)输出 $output = curl_exec($ch); var_dump($output); curl_close($ch); /* * 想帅的能够利用 JSON 函数 json_decode(仅处理 UTF-8 编码数据) 来美化输出 * 当函数 assoc 参数为 true 返回的是 array, 反之是 object, 默认为 false * */ $json_output = json_decode($output); var_dump($json_output);
<?php $appid = ""; $appsecret = ""; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret"; // file_get_contents 将整个文件读入一个字符串中。 $output = file_get_contents($url); $json_output = json_decode($output, true); var_dump($json_output);
string(194) "{"access_token":"11_-S30IWoUhYZvZw2Qe......","expires_in":7200}"
object(stdClass)#6 (2) { ["access_token"]=> string(157) "11_AkasWeD0okdTqXDyqw4......" ["expires_in"]=> int(7200) }
array(2) { ["access_token"]=> string(136) "11_OuFwGg-aW8y6EC1Gt1dVi......" ["expires_in"]=> int(7200) }