Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148398
  • 博文数量: 44
  • 博客积分: 2330
  • 博客等级: 大尉
  • 技术积分: 405
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 11:57
文章分类
文章存档

2012年(6)

2011年(32)

2010年(6)

分类:

2010-12-25 17:55:39

/**
 * 将常用日期格式转化为unix时间戳
 */
function GmmktimeFromDate($date) {
    $temp1=explode(" ",$date);
    $temp2=explode("-",$temp1[0]);
    $temp3=explode(":",$temp1[1]);
    if($temp2[0]<=0) $temp2[0]=0;
    if($temp2[1]<=0) $temp2[1]=0;
    if($temp2[2]<=0) $temp2[2]=0;
    if($temp3[0]<=0) $temp3[0]=0;
    if($temp3[1]<=0) $temp3[1]=0;
    if($temp3[2]<=0) $temp3[2]=0;
    return @mktime($temp3[0],$temp3[1],$temp3[2],$temp2[1],$temp2[2],$temp2[0]);
}


/**
 * 函数功能:下载网络图片,返回值:数组(包括图片路径,大小,*)
 */
function GrabImage($url,$i) {
    ob_start();
    if($url=="") {
        return false;
    }
    $ext=strrchr($url,".");
    if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") {
        return false;
    }
    $filename=$i.$ext;
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $size = strlen($img);
    $fp2=@fopen("getimage/".$filename, "a"); //此处可加上图片存放路径
    fwrite($fp2,$img);
    fclose($fp2);
    $imgwh=getimagesize("getimage/".$filename);
    return array($filename,$size,"{$imgwh[0]}*{$imgwh[1]}");
}


/**
 * 下载文件到本地
 */
function download($swf_url) {
    $destination_folder = 'swf/'; // 文件夹保存下载文件。必须以斜杠结尾
    $newfname = time().basename($swf_url);
    $file = @fopen ($swf_url, "rb");
    if ($file) {
        $newf = @fopen ($destination_folder.$newfname, "wb");
        if ($newf)
            while(!feof($file)) {
                fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
            }
    }else {
        return false;
    }
    if ($file) {
        fclose($file);
    }
    if ($newf) {
        fclose($newf);
    }
    return $newfname;
}


/**
 * 获取用户的真实IP地址
 *
 */
function get_real_ip() {
    $ip=false;
    if(!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = FALSE;
        }
        for ($i = 0; $i < count($ips); $i++) {
            if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}


/**
 * 获取URL内容
 */
function get_page_content($url) {
    $url = eregi_replace('^http://', '', $url);
    $temp = explode('/', $url);
    $host = array_shift($temp);
    $path = '/'.implode('/', $temp);
    $temp = explode(':', $host);
    $host = $temp[0];
    $port = isset($temp[1]) ? $temp[1] : 80;
    $fp = @fsockopen($host, $port, &$errno, &$errstr, 30);
    if ($fp) {
        @fputs($fp, "GET $path HTTP/1.1\r\nHost: $host\r\nAccept: */*\r\nReferer:$url\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\nConnection: Close\r\n\r\n");
    }
    $Content = "";
    while ($str = @fread($fp, 4096)) {
        $Content .= $str;
    }
    @fclose($fp);
    //重定向
    if(preg_match("/^HTTP\/\d.\d 301 Moved Permanently/is",$Content)) {
        if(preg_match("/Location:(.*?)\r\n/is",$Content,$murl)) {
            return get_page_content($murl[1]);
        }
    }
    //读取内容
    if(preg_match("/^HTTP\/\d.\d 200 OK/is",$Content)) {
        preg_match("/Content-Type:(.*?)\r\n/is",$Content,$murl);
        $contentType=trim($murl[1]);
        $Content=explode("\r\n\r\n",$Content,2);
        $Content=$Content[1];
    }
    return $Content;
}


阅读(1096) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~