php 模拟get_headers函数

 
更多

php 模拟get_headers函数。具体代码如下:

<?php
if(!function_exists('get_headers')){
    function get_headers($url,$format=0){
        $url=parse_url($url);
        $end="\r\n\r\n";
        $fp=fsockopen($url['host'],(empty($url['port'])?80:$url['port']),$errno,$errstr,30);
        if($fp){
            $out="GET / HTTP/1.1\r\n";
            $out.="Host: ".$url['host']."\r\n";
            $out.="Connection: Close\r\n\r\n";
            $var='';
            fwrite($fp,$out);
            while(!feof($fp)){
                $var.=fgets($fp,1280);
                if(strpos($var,$end))
                break;
            }
            fclose($fp);
            $var=preg_replace("/\r\n\r\n.*\$/",'',$var);
            $var=explode("\r\n",$var);
            if($format){
                foreach($var as $i){
                    if(preg_match('/^([a-zA-Z -]+): +(.*)$/',$i,$parts))
                    $v[$parts[1]]=$parts[2];
                }
                return $v;
            }else{
				return $var;
			}
        }
    }
}
echo '<pre>';
print_r(get_headers('http://www.phpernote.com/php-regular-expression/49.html'));
打赏

本文固定链接: https://www.cxy163.net/archives/4129 | 绝缘体

该日志由 绝缘体.. 于 2013年05月23日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: php 模拟get_headers函数 | 绝缘体
关键字: , , , ,

php 模拟get_headers函数:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter