Home > php教程 > php手册 > body text

php 模拟get_headers函数的代码示例

WBOY
Release: 2016-06-13 11:53:28
Original
1012 people have browsed it

复制代码 代码如下:


02 if(!function_exists('get_headers')){ 

03     function get_headers($url,$format=0){ 

04         $url=parse_url($url); 

05         $end="\r\n\r\n"; 

06         $fp=fsockopen($url['host'],(empty($url['port'])?80:$url['port']),$errno,$errstr,30); 

07         if($fp){ 

08             $out="GET / HTTP/1.1\r\n"; 

09             $out.="Host: ".$url['host']."\r\n"; 

10             $out.="Connection: Close\r\n\r\n"; 

11             $var=''; 

12             fwrite($fp,$out); 

13             while(!feof($fp)){ 

14                 $var.=fgets($fp,1280); 

15                 if(strpos($var,$end)) 

16                 break; 

17             } 

18             fclose($fp); 

19             $var=preg_replace("/\r\n\r\n.*\$/",'',$var); 

20             $var=explode("\r\n",$var); 

21             if($format){ 

22                 foreach($var as $i){ 

23                     if(preg_match('/^([a-zA-Z -]+): +(.*)$/',$i,$parts)) 

24                     $v[$parts[1]]=$parts[2]; 

25                 } 

26                 return $v; 

27             }else{ 

28                 return $var; 

29             } 

30         } 

31     } 

32 } 

33 echo '

';  
<p>34 print_r(get_headers('http://www.jb51.net')); <br></p>

Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!