array( 'protocol_version'=>'1.0', //http协议版本(若不指定php5.2系默认为http1.0) 'method'=>"GET",//获取方式 'timeout' => $timeout ,//超时时间 'header'=> $header) ); $context = stream_context_create($opts); return @file_get_contents($url,false,$context); } /** * file_get_contents POST数据 * @param string $url 发送地址 * @param array $post_data 发送数组 * @param integer $timeout 超时秒 * @param string $header 头信息 * @return string */ static public function phpPost($url, $post_data=array(), $timeout=5,$header="") { $header=empty($header)?self::defaultHeader():$header; $post_string = http_build_query($post_data); $header.="Content-length: ".strlen($post_string); $opts = array( 'http'=>array( 'protocol_version'=>'1.0',//http协议版本(若不指定php5.2系默认为http1.0) 'method'=>"POST",//获取方式 'timeout' => $timeout ,//超时时间 'header'=> $header, 'content'=> $post_string) ); $context = stream_context_create($opts); return @file_get_contents($url,false,$context); } /** * 默认HTTP头 * @return string */ static private function defaultHeader() { $header="User-Agent:Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12\r\n"; $header.="Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"; $header.="Accept-language: zh-cn,zh;q=0.5\r\n"; $header.="Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n"; return $header; } /** * 获取通过socket方式get和post页面的返回数据 * @param string */ static private function GetHttpContent($fsock=null) { $out = null; while($buff = @fgets($fsock, 2048)){ $out .= $buff; } fclose($fsock); $pos = strpos($out, "\r\n\r\n"); $head = substr($out, 0, $pos); //http head $status = substr($head, 0, strpos($head, "\r\n")); //http status line $body = substr($out, $pos + 4, strlen($out) - ($pos + 4));//page body if(preg_match("/^HTTP\/\d\.\d\s([\d]+)\s.*$/", $status, $matches)) { if(intval($matches[1]) / 100 == 2) { return $body; } else { return false; } } else { return false; } } /** * 下载文件 * @param string $filename 文件名 * @param string $showname 显示文件名 * @param integer $expire 缓存时间 * @return boolean */ static public function download($filename, $showname='',$expire=1800) { if(file_exists($filename)&&is_file($filename)) { $length = filesize($filename); }else { die('下载文件不存在!'); } //$finfo = new \finfo(FILEINFO_MIME); //$type = $finfo->file($filename); $type = pathinfo($filename, PATHINFO_EXTENSION); //发送Http Header信息 开始下载 header("Pragma: public"); header("Cache-control: max-age=".$expire); //header('Cache-Control: no-store, no-cache, must-revalidate'); header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT"); header("Content-Disposition: attachment; filename=".$showname); header("Content-Length: ".$length); header("Content-type: ".$type); header('Content-Encoding: none'); header("Content-Transfer-Encoding: binary" ); readfile($filename); return true; } }