|
- <?php
- function http_request($url,$data=null){
- $ch=curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- if (!empty($data)){
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- $output=curl_exec($ch);
- curl_close($ch);
- return $output;
- }
- function write_log($activation){
-
- $dir=CONFIG_ADDR."/../Log";
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $open=fopen($dir."/Function_log.txt","a");
- fwrite($open,date("Y-m-d H:i:s")."\t".$activation."\r\n");
- fclose($open);
- }
- function downloadImageFromWeiXin($url){
- $ch=curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_NOBODY, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
- $body=curl_exec($ch);
- $header=curl_getinfo($ch);
- curl_close($ch);
- return array_merge(array('body'=>$body),array('header'=>$header));
- }
- function downfile($file)
- {
- $date=basename($file);
- Header( "Content-type: image/png");
- Header( "Accept-Ranges: bytes ");
- Header( "Accept-Length: " .filesize($file));
- header( "Content-Disposition: attachment; filename= {$date}");
- readfile($file);
- }
- function getAccesstoken(){
- $dir=CONFIG_ADDR."/../Log";
-
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $token_file=$dir.'/access_token';
- if (file_exists($token_file) && time()-filemtime($token_file)<4800){
- write_log("menu--文件中读取的token:".file_get_contents($token_file));
- $access_token= file_get_contents($token_file);
- }else{
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
- $result=http_request($url);
- if(!$result){
- write_log("menu-获取token出错");
- echo "menu-获取token出错";
- exit;
- }
-
- $result_obj=json_decode($result);
- file_put_contents($token_file, $result_obj->access_token);
- write_log("menu-url获取的token:".$result_obj->access_token);
- $access_token= $result_obj->access_token;
- }
- return $access_token;
- }
|