|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?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(){
- // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
- $data = json_decode(get_php_file("access_token.php"));
- if ($data->expire_time < time()) {
- // 如果是企业号用以下URL获取access_token
- // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
- $res = json_decode(http_request($url));
- $access_token = $res->access_token;
- write_log("url获取的token:".$access_token);
- if ($access_token) {
- $data->expire_time = time() + 3600;
- $data->access_token = $access_token;
- set_php_file("access_token.php", json_encode($data));
- write_log("生成access_token:".$access_token);
- }
- } else {
- $access_token = $data->access_token;
- write_log("文件中读取的token:".$access_token);
- }
- /* $dir=__DIR__."/../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;
- }
- function get_php_file($filename) {
- $dir=__DIR__."/../Log/";
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $token_file=$dir.$filename;
- return trim(substr(file_get_contents($token_file), 15));
- }
- function set_php_file($filename, $content) {
- $dir=__DIR__."/../Log/";
- if (!is_dir($dir)){
- mkdir($dir);
- }
- $token_file=$dir.$filename;
- $fp = fopen($token_file, "w");
- fwrite($fp, "<?php exit();?>" . $content);
- fclose($fp);
- }
-
- function convert_baidumap_to_tencentmap( &$lat, &$lng ) {
- $x_pi = 3.14159265358979324 * 3000.0 / 180.0;
- $x = $lng - 0.0065;
- $y = $lat - 0.006;
- $z = sqrt(($x*$x+$y*$y)) - 0.00002 * sin($y * $x_pi);
- $theta = atan2($y,$x) - 0.000003 * cos($x * $x_pi);
- $lng = $z * cos($theta);
- $lat = $z * sin($theta);
- }
-
-
|