How to retrieve headers using the getHeaders function?

更新于
2026-09-27 13:15:59
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计147个文字,预计阅读时间需要1分钟。

How to retrieve headers using the getHeaders function?

phpfunction getHeaders($url, $assoc=false) { $timeout=5; $buf=array(); $parse=parse_url($url); $fp=fsockopen($parse['host'], isset($parse['port']) ? $parse['port'] : 80, $errno, $errstr, $timeout); $path=isset($parse['path']) ? $parse['path'] : '';}

How to retrieve headers using the getHeaders function?

<?php function getHeaders($url, $assoc = false) { $timeout = 5; $buf = array(); $parse = parse_url($url); $fp = fsockopen($parse['host'], isset($parse['port']) ? $parse['port'] : 80, $errno, $errstr, $timeout); $path = isset($parse['path']) ? $parse['path'] . ( isset($parse['query']) ? '?' . $parse['query'] : '' ) : '/'; $headers = 'GET '. $path .' HTTP/1.1'."\\r\\n". 'Host: '. $parse['host'] ."\\r\\n". 'Connection: close'."\\r\\n". 'Accept: */*' . "\\r\\n". 'User-Agent: Mozilla/5.0'. "\\r\\n\\r\\n"; fputs($fp, $headers); while(!feof($fp)) { if(($readline = fgets($fp, 4096)) && in_array($readline, array("\\n", "\\r\\n"))) break; if(!$assoc) { array_push($buf, $readline); }else{ if(preg_match('#(\\w+)\\s*:(.+)#', $readline, $match)) $buf[$match[1]] = trim($match[2]); array_push($buf, $readline); } } fclose($fp); return $buf; }

本文共计147个文字,预计阅读时间需要1分钟。

How to retrieve headers using the getHeaders function?

phpfunction getHeaders($url, $assoc=false) { $timeout=5; $buf=array(); $parse=parse_url($url); $fp=fsockopen($parse['host'], isset($parse['port']) ? $parse['port'] : 80, $errno, $errstr, $timeout); $path=isset($parse['path']) ? $parse['path'] : '';}

How to retrieve headers using the getHeaders function?

<?php function getHeaders($url, $assoc = false) { $timeout = 5; $buf = array(); $parse = parse_url($url); $fp = fsockopen($parse['host'], isset($parse['port']) ? $parse['port'] : 80, $errno, $errstr, $timeout); $path = isset($parse['path']) ? $parse['path'] . ( isset($parse['query']) ? '?' . $parse['query'] : '' ) : '/'; $headers = 'GET '. $path .' HTTP/1.1'."\\r\\n". 'Host: '. $parse['host'] ."\\r\\n". 'Connection: close'."\\r\\n". 'Accept: */*' . "\\r\\n". 'User-Agent: Mozilla/5.0'. "\\r\\n\\r\\n"; fputs($fp, $headers); while(!feof($fp)) { if(($readline = fgets($fp, 4096)) && in_array($readline, array("\\n", "\\r\\n"))) break; if(!$assoc) { array_push($buf, $readline); }else{ if(preg_match('#(\\w+)\\s*:(.+)#', $readline, $match)) $buf[$match[1]] = trim($match[2]); array_push($buf, $readline); } } fclose($fp); return $buf; }