• php 原生或curl获取 http headers


    有一个函数:

    array get_headers ( string $url [, int $format = 0 ] )

    Parameters

    url
      The target URL.

    format
      If the optional format parameter is set to non-zero, get_headers() parses the response and sets the array's keys.

      设置为非0返会解析响应关联数组。
    Return Values

      Returns an indexed or associative array with the headers, or FALSE on failure.

    <?php
    $url 
    'http://www.example.com';

    print_r(get_headers($url));

    print_r(get_headers($url1));
    ?>

    Array
    (
        [0] => HTTP/1.1 200 OK
        [1] => Date: Sat, 29 May 2004 12:28:13 GMT
        [2] => Server: Apache/1.3.27 (Unix)  (Red-Hat/Linux)
        [3] => Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
        [4] => ETag: "3f80f-1b6-3e1cb03b"
        [5] => Accept-Ranges: bytes
        [6] => Content-Length: 438
        [7] => Connection: close
        [8] => Content-Type: text/html
    )
    
    Array
    (
        [0] => HTTP/1.1 200 OK
        [Date] => Sat, 29 May 2004 12:28:14 GMT
        [Server] => Apache/1.3.27 (Unix)  (Red-Hat/Linux)
        [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
        [ETag] => "3f80f-1b6-3e1cb03b"
        [Accept-Ranges] => bytes
        [Content-Length] => 438
        [Connection] => close
        [Content-Type] => text/html
    )

    http://php.net/manual/en/function.get-headers.php

    array getallheaders ( void )

    Fetches all HTTP headers from the current request.

    This function is an alias for apache_request_headers(). Please read the apache_request_headers() documentation for more information on how this function works.

    An associative array of all the HTTP headers in the current request, or FALSE on failure.

    我们可以自己写这个函数:

      在PHP里,想要得到所有的HTTP请求头,可以使用getallheaders方法,不过此方法并不是在任何环境下都存在,比如说,你使用fastcgi方式运行PHP的话,就没有这个方法,所以说我们还需要考虑别的方法,幸运的是$_SERVER里有我们想要的东西,它里面键名以HTTP_开头的就是HTTP请求头:

    $headers = array(); 
    foreach (
    $_SERVER as $key => $value) { 
        if (
    'HTTP_' == substr($key05)) { 
            
    $headers[str_replace('_''-'substr($key5))] = $value
        } 
    }


    代码很简单,需要说明的是RFC里明确指出了信息头的名字是不区分大小写的。

    不过并不是所有的HTTP请求头都是以HTTP_开头的的键的形式存在与$_SERVER里,比如说Authorization,Content-Length,Content-Type就不是这样,所以说为了取得所有的HTTP请求头,还需要加上下面这段代码:

    if (isset($_SERVER['PHP_AUTH_DIGEST'])) { 
        $header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST']); 
    } elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { 
        $header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'])); 
    } 
    if (isset($_SERVER['CONTENT_LENGTH'])) { 
        $header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH']; 
    } 
    if (isset($_SERVER['CONTENT_TYPE'])) { 
        $header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE']; //注意-和下划线不同
    }

    http://php.net/manual/zh/function.getallheaders.php

    我用$_SERVER['CONTENT_TYPE']出现

    Undefined index: CONTENT_TYPE。

    curl设置header:http://blog.51yip.com/php/1297.html

    curl获取content type

    <?php
      # the request
      $ch = curl_init('http://www.google.com');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_exec($ch);
    
      # get the content type
      echo curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
    
      # output
      text/html; charset=ISO-8859-1
    ?>

    参考:http://stackoverflow.com/questions/2610713/get-mime-type-of-external-file-using-curl-and-php

    我们curl_getinfo如果后面只有一个参数,返回一个array

    $httpinfo=curl_getinfo($ch);
    print_r( $httpinfo);

    返回如:

    Array
    (
        [url] => http://www.php10086.com/2012/04/605.html
        [content_type] => text/html; charset=UTF-8
        [http_code] => 200
        [header_size] => 344
        [request_size] => 71
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 1.545
        [namelookup_time] => 0
        [connect_time] => 0.172
        [pretransfer_time] => 0.172
        [size_upload] => 0
        [size_download] => 54618
        [speed_download] => 35351
        [speed_upload] => 0
        [download_content_length] => -1
        [upload_content_length] => 0
        [starttransfer_time] => 0.593
        [redirect_time] => 0
        [certinfo] => Array
            (
            )
    
        [primary_ip] => 173.254.29.116
        [primary_port] => 80
        [local_ip] => 192.168.1.5
        [local_port] => 49970
        [redirect_url] => 
    )
  • 相关阅读:
    【BZOJ 3238】 3238: [Ahoi2013]差异(SAM)
    【BZOJ 4180】 4180: 字符串计数 (SAM+二分+矩阵乘法)
    【BZOJ 3676】 3676: [Apio2014]回文串 (SAM+Manacher+倍增)
    【BZOJ 3998】 3998: [TJOI2015]弦论 (SAM )
    【BZOJ 2946】 2946: [Poi2000]公共串 (SAM)
    【BZOJ 1398】 1398: Vijos1382寻找主人 Necklace (最小表示法)
    【BZOJ 4031】 4031: [HEOI2015]小Z的房间 (Matrix-Tree Theorem)
    【BZOJ 3534】 3534: [Sdoi2014]重建 (Matrix-Tree Theorem)
    【BZOJ 3659】 3659: Which Dreamed It (Matrix-Tree&BEST theorem )
    【BZOJ 4596】 4596: [Shoi2016]黑暗前的幻想乡 (容斥原理+矩阵树定理)
  • 原文地址:https://www.cnblogs.com/youxin/p/3242261.html
Copyright © 2020-2023  润新知