1 | <?php |
||
18 | final class headers |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * Parse response headers string from a HTTP request into an array of headers. e.g. |
||
23 | * [ 'Location' => 'http://www.example.com', ... ] |
||
24 | * When multiple headers with the same name are present, all values will form an array, in the order in which |
||
25 | * they are present in the source. |
||
26 | * @param string $headers The headers string to parse. |
||
27 | * @return array |
||
28 | */ |
||
29 | 2 | public static function parse( $headers ) { |
|
30 | 2 | if ( !is_array($headers) && !$headers instanceof \ArrayObject ) { |
|
31 | $headers = array_filter( |
||
32 | array_map( "trim", explode( "\n", (string) $headers ) ) |
||
|
|||
33 | ); |
||
34 | } |
||
35 | 2 | $result = []; |
|
36 | 2 | foreach( $headers as $header ) { |
|
37 | 2 | $temp = array_map('trim', explode(':', $header, 2) ); |
|
38 | 2 | if ( isset( $temp[1] ) ) { |
|
39 | 2 | if ( !isset($result[ $temp[0]]) ) { |
|
40 | // first entry for this header |
||
41 | 2 | $result[ $temp[0] ] = $temp[1]; |
|
42 | } else if ( is_string($result[ $temp[0] ]) ) { |
||
43 | // second header entry with same name |
||
44 | $result[ $temp[0] ] = [ |
||
45 | $result[ $temp[0] ], |
||
46 | $temp[1] |
||
47 | ]; |
||
48 | } else { // third or later header entry with same name |
||
49 | 2 | $result[ $temp[0] ][] = $temp[1]; |
|
50 | } |
||
51 | } else { // e.g. HTTP1/1 200 OK |
||
52 | 2 | $result[] = $temp[0]; |
|
53 | } |
||
54 | } |
||
55 | 2 | return $result; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Return the last value sent for a specific header, uses the output of parse(). |
||
60 | * @param (mixed) $headers An array with multiple header strings or a single string. |
||
61 | * @return array|mixed |
||
62 | */ |
||
63 | 1 | private static function getLastHeader($headers) { |
|
69 | |||
70 | /** |
||
71 | * Parse response headers to determine if and how long you may cache the response. Doesn't understand ETags. |
||
72 | * @param mixed $headers Headers string or array as returned by parse() |
||
73 | * @param bool $private Whether to store a private cache or public cache image. |
||
74 | * @return int The number of seconds you may cache this result starting from now. |
||
75 | */ |
||
76 | 1 | public static function parseCacheTime( $headers, $private=true ) { |
|
125 | |||
126 | } |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.