以下代码都是本人在工作中遇到的问题,并完成的具体代码和注释,不多说,直接上代码:
<?php //组织链接 $dataurl = "http://118.194.236.54:888/kw/"; $date = date("Y-m-d",strtotime("-1 day")); $fileName = $date . ".tar.gz"; $dataurl = $dataurl . $fileName; //下载昨天数据,如果curl方法可用,默认使用curl方法! function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo($url, PATHINFO_BASENAME) : $file; $dir = pathinfo($file, PATHINFO_DIRNAME); !is_dir($dir) && @mkdir($dir, 0755, true); $url = str_replace(" ", "%20", $url); if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $temp = curl_exec($ch); if(@file_put_contents($file, $temp) && !curl_error($ch)) { return $file; } else { return false; } } else { $opts = array( "http" => array( "method" => "GET", "header" => "", "timeout" => $timeout) ); $context = stream_context_create($opts); if(@copy($url, $file, $context)) { return $file; } else { return false; } } } if(!httpcopy($dataurl, "./" . $fileName, 60)) { echo "下载出错!"; } //解压tar.gz文件 $phar = new PharData($fileName); $phar->extractTo('./', null, true); //读取解压后的文件数据 $handle = fopen($date . ".txt", "r"); $text = ''; while (!feof($handle)) { $buffer = fgets($handle); $res = explode("\t", $buffer); $text .= $res[1] . ' '; } fclose($handle); //写入关键字文件 $name = $date . "keyword.txt"; $fp = fopen($name, "w+"); fwrite($fp, $text); fclose($fp); ?>