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) }