You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

74 lines
2.1 KiB

  1. <?php
  2. function http_request($url,$data=null){
  3. $ch=curl_init();
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  6. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  7. if (!empty($data)){
  8. curl_setopt($ch, CURLOPT_POST, 1);
  9. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  10. }
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  12. $output=curl_exec($ch);
  13. curl_close($ch);
  14. return $output;
  15. }
  16. function write_log($activation){
  17. $dir=CONFIG_ADDR."/../Log";
  18. if (!is_dir($dir)){
  19. mkdir($dir);
  20. }
  21. $open=fopen($dir."/Function_log.txt","a");
  22. fwrite($open,date("Y-m-d H:i:s")."\t".$activation."\r\n");
  23. fclose($open);
  24. }
  25. function downloadImageFromWeiXin($url){
  26. $ch=curl_init();
  27. curl_setopt($ch, CURLOPT_URL, $url);
  28. curl_setopt($ch, CURLOPT_HEADER, 0);
  29. curl_setopt($ch, CURLOPT_NOBODY, 0);
  30. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  31. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  33. $body=curl_exec($ch);
  34. $header=curl_getinfo($ch);
  35. curl_close($ch);
  36. return array_merge(array('body'=>$body),array('header'=>$header));
  37. }
  38. function downfile($file)
  39. {
  40. $date=basename($file);
  41. Header( "Content-type: image/png");
  42. Header( "Accept-Ranges: bytes ");
  43. Header( "Accept-Length: " .filesize($file));
  44. header( "Content-Disposition: attachment; filename= {$date}");
  45. readfile($file);
  46. }
  47. function getAccesstoken(){
  48. $dir=CONFIG_ADDR."/../Log";
  49. if (!is_dir($dir)){
  50. mkdir($dir);
  51. }
  52. $token_file=$dir.'/access_token';
  53. if (file_exists($token_file) && time()-filemtime($token_file)<4800){
  54. write_log("menu--文件中读取的token:".file_get_contents($token_file));
  55. $access_token= file_get_contents($token_file);
  56. }else{
  57. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
  58. $result=http_request($url);
  59. if(!$result){
  60. write_log("menu-获取token出错");
  61. echo "menu-获取token出错";
  62. exit;
  63. }
  64. $result_obj=json_decode($result);
  65. file_put_contents($token_file, $result_obj->access_token);
  66. write_log("menu-url获取的token:".$result_obj->access_token);
  67. $access_token= $result_obj->access_token;
  68. }
  69. return $access_token;
  70. }