加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php 模拟get_headers函数的代码示例

发布时间:2020-12-13 06:16:11 所属栏目:PHP教程 来源:网络整理
导读:代码如下: 02 if(!function_exists('get_headers')){ 03 function get_headers($url,$format=0){ 04 $url=parse_url($url); 05 $end="rnrn"; 06 $fp=fsockopen($url['host'],(empty($url['port'])?80:$url['port']),$errno,$errstr,30); 07 if($fp){ 08

代码如下:

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

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

04 $url=parse_url($url);

05 $end="rnrn";

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

07 if($fp){

08 $out="GET / HTTP/1.1rn";

09 $out.="Host: ".$url['host']."rn";

10 $out.="Connection: Closernrn";

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("/rnrn.*$/",'',$var);

20 $var=explode("rn",$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 '

'; 

34 print_r(get_headers('//www.52php.cn'));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读