Browse Source

聊天记录下载

lizhen_gitee 1 year ago
parent
commit
2a351bdc2a
2 changed files with 56 additions and 3 deletions
  1. 30 0
      application/common.php
  2. 26 3
      application/index/controller/Test.php

+ 30 - 0
application/common.php

@@ -839,4 +839,34 @@ if(!function_exists('touch_file')) {
             }
         }
     }
+}
+
+
+//下载远程图片 到指定目录
+function downloadfile($file_url, $path = '', $save_file_name = '')
+{
+    $basepath = '/uploaded/';
+    if ($path) {
+        $basepath = $basepath . $path . '/';
+    }
+    $basepath = $basepath . date('Ymd');
+    $dir_path = ROOT_PATH . '/public' . $basepath;
+    if (!is_dir($dir_path)) {
+        mkdir($dir_path, 0777, true);
+    }
+
+
+    $file = file_get_contents($file_url);
+
+
+    //传入保存文件的名称
+    $filename = $save_file_name ?: pathinfo($file_url, PATHINFO_BASENAME);
+
+    $resource = fopen($dir_path. '/'. $filename, 'a');
+
+    fwrite($resource, $file);
+
+    fclose($resource);
+
+    return $dir_path . '/' . $filename;
 }

+ 26 - 3
application/index/controller/Test.php

@@ -38,9 +38,9 @@ class Test extends Controller
 
         $data = [
             'ChatType' => 'Group',
-//            'ChatType' => 'C2C',
-            'MsgTime'  => date('YmdH',strtotime('-1 Days')),
-//            'MsgTime'  => date('YmdH',strtotime('-60 Hours')),
+            'ChatType' => 'C2C',
+//            'MsgTime'  => date('YmdH',strtotime('-1 Days')),
+            'MsgTime'  => date('YmdH',strtotime('-20 Hours')),
         ];
         dump($data);
         $jsonStr = json_encode($data);
@@ -50,10 +50,33 @@ class Test extends Controller
             'Content-Length: ' . strlen($jsonStr)
         );
         $rs = curl_post($url,$jsonStr,$header);
+        $rs = json_decode($rs,true);
         dump($rs);
+        if(is_array($rs) && isset($rs['ErrorCode']) && $rs['ErrorCode'] == 0){
+            //正常的,可以下载了
+            if(isset($rs['File']) && !empty($rs['File'])){
+                foreach($rs['File'] as $key => $val){
+                    //下载
+                    $newgz = downloadfile($val['URL'],$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
+                    echo $newgz;
+                    //解压
+                    //分析
+                    //入库
+                }
+            }
+        }
 
+    }
 
+    //测试下载gz
+    public function test11(){
+        $data = [
+            'ChatType' => 'C2C',
+            'MsgTime'  => date('YmdH',strtotime('-20 Hours')),
+        ];
 
+        $url = 'https://download.tim.qq.com/msg_history/v2/e9c80dce105d880ab8f62f0d267983c0edf3aff49031f5b06204c3402ddbdaf63a7b2aed.gz';
+        $newgz = downloadfile($url,$data['ChatType'],$data['MsgTime'].'_'.$data['ChatType'].'.json.gz');
     }
 
     private function usersig($sdkappid,$key){