PHP微信開發-access-token的獲取與存儲

存儲是以.txt的方式存儲token值的 先上代碼


 
  1. <?php

  2.  
  3. require_once("file_cache.php");

  4. class WeixinUtil{

  5. private $appId = 'appId ';

  6. private $appSecret = 'appSecret ';

  7. //獲取AccessToken

  8. public function getAccessToken(){

  9. $wx_access_token_cache_key = 'wx_access_token';

  10.  
  11. $cache = new FileCache('./access_token.txt');//存儲的本地位置

  12. $token = $cache->get($wx_access_token_cache_key);//去文件中獲取token

  13.  
  14. if (!$token){//沒有的話就創建新的

  15. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;

  16. $token = $this->https_curl_json($url,null);

  17. $cache->set($wx_access_token_cache_key, $token, time()+7000);

  18. }

  19.  
  20. $token = json_decode($token);

  21. $token = $token->access_token;

  22. return $token;

  23. }

  24.  
  25. /* 發送json格式的數據,到api接口*/

  26. public function https_curl_json($url,$data){

  27. $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");

  28. $curl = curl_init();

  29. curl_setopt($curl, CURLOPT_URL, $url);

  30. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

  31. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

  32. if (!empty($data)){

  33. curl_setopt($curl, CURLOPT_POST, 1);

  34. curl_setopt($curl, CURLOPT_POSTFIELDS,$data);

  35. }

  36. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  37. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );

  38. $output = curl_exec($curl);

  39. if (curl_errno($curl)) {

  40. echo 'Errno'.curl_error($curl);//捕抓異常

  41. }

  42. curl_close($curl);

  43. return $output;

  44. }

  45.  
  46. }

  47. ?>

另一個類 用來進行文件操作


 
  1. <?php

  2. //該類用來配合WeixinUtil.php類 進行token的文件操作

  3. class FileCache{

  4. private $cache_file;

  5.  
  6. private function load(){

  7. if(file_exists($this->cache_file)){

  8. $content = file_get_contents($this->cache_file);

  9. if (strlen($content) > 0){

  10. $data = json_decode($content);

  11. return $data;

  12. }

  13. }

  14. return array();

  15. }

  16.  
  17. private function save($data){

  18. $content = json_encode($data);

  19. return file_put_contents($this->cache_file, $content);

  20. }

  21.  
  22. public function __construct($filename) {

  23. $this->cache_file = $filename;

  24. }

  25.  
  26. public function get($key){

  27. $data = $this->load();

  28.  
  29. foreach($data as $item){

  30. if ($item->key == $key){

  31. if ($item->expire_time > time()){

  32. return $item->value;

  33. }

  34. break;

  35. }

  36. }

  37. return NULL;

  38. }

  39.  
  40. public function set($key, $value, $expire_time=NULL){

  41. $data = $this->load();

  42. $obj = NULL;

  43. foreach($data as $item){

  44. if ($item->key == $key){

  45. $obj = $item;

  46. $obj->value = $value;

  47. if ($expire_time != NULL){

  48. $obj->expire_time = $expire_time;

  49. }

  50. break;

  51. }

  52. }

  53.  
  54. if ($obj == NULL){

  55. $obj = new CacheItem($key, $value, $expire_time);

  56. array_push($data, $obj);

  57. }

  58. return $this->save($data);

  59. }

  60. }

  61.  
  62. class CacheItem{

  63. public $key;

  64. public $value;

  65. public $expire_time;

  66.  
  67. public function __construct($key, $value, $expire_time) {

  68. $this->key = $key;

  69. $this->value = $value;

  70. $this->expire_time = $expire_time;

  71. }

  72. }

  73. ?>

最後 測試類


 
  1. <?php

  2. include "WeixinUtil.php";

  3. $wx = new WeixinUtil();

  4. echo $wx->getAccessToken();

第二個類來自https://www.cnblogs.com/hydonlee/p/5419063.html

 

=======================================================================================

18/9/11

access_token存儲在DB裏,參考文章↓

https://blog.csdn.net/sct_t/article/details/53002611

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章