当前位置 : 主页 > 网络编程 > PHP >

getHeaders函数

来源:互联网 收集:自由互联 发布时间:2021-07-03
?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'])
 
<?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;
      
}

网友评论