function.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. // 获取请求的地址的方法
  3. if(!function_exists("http_curl")){
  4. function http_curl($url,$data =array(),$method ="get",$returnType ="json")
  5. {
  6. //1.开启会话
  7. $ch = curl_init();
  8. //2.设置参数
  9. curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
  10. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  11. if($method!="get"){
  12. curl_setopt($ch,CURLOPT_POST,TRUE);
  13. curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
  14. }
  15. curl_setopt($ch,CURLOPT_URL,$url);
  16. //执行会话
  17. $json = curl_exec($ch);
  18. curl_close($ch);
  19. if($returnType == "json"){
  20. return json_decode($json,true);
  21. }
  22. return $json;
  23. }
  24. }
  25. if(!function_exists('get_access_token')){
  26. function get_access_token()
  27. {
  28. $appid = "wx6ddfdda51d6a1a19"; //微信的appid
  29. $secret ="00a7281205f582fb08d2d3bd9552cb03"; //微信的开发者密钥
  30. // 读取缓存中的内容
  31. include_once "MyMemcache.php"; //引入缓存方法文件
  32. $obj = new \MyMemcache();
  33. $value = $obj ->get($appid);
  34. if(!$value){
  35. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
  36. $result = http_curl($url);
  37. $value = $result['access_token'];
  38. $obj->set($appid,$value,7000);
  39. }
  40. return $value;
  41. }
  42. }