1 | <?php |
||
10 | final class Http |
||
11 | { |
||
12 | /** |
||
13 | * Parses HTTP headers into an associative array. |
||
14 | * |
||
15 | * Example: |
||
16 | * <code> |
||
17 | * $headers = "HTTP/1.1 200 OK\r\n". |
||
18 | * "content-type: text/html; charset=UTF-8\r\n". |
||
19 | * "Server: Funky/1.0\r\n". |
||
20 | * "Set-Cookie: foo=bar\r\n". |
||
21 | * "Set-Cookie: baz=quux\r\n". |
||
22 | * "Folds: are\r\n\treformatted\r\n"; |
||
23 | * print_r(\TraderInteractive\HttpUtil::parseHeaders($headers)); |
||
24 | * </code> |
||
25 | * The above example will output: |
||
26 | * <pre> |
||
27 | * Array |
||
28 | * ( |
||
29 | * [Response Code] => 200 |
||
30 | * [Response Status] => OK |
||
31 | * [Content-Type] => text/html; charset=UTF-8 |
||
32 | * [Server] => Funky/1.0 |
||
33 | * [Set-Cookie] => Array |
||
34 | * ( |
||
35 | * [0] => foo=bar |
||
36 | * [1] => baz=quux |
||
37 | * ) |
||
38 | * [Folds] => are reformatted |
||
39 | * ) |
||
40 | * </pre> |
||
41 | * |
||
42 | * @param string $rawHeaders string containing HTTP headers |
||
43 | * |
||
44 | * @return array the parsed headers |
||
45 | * |
||
46 | * @throws Exception Thrown if unable to parse the headers |
||
47 | */ |
||
48 | public static function parseHeaders(string $rawHeaders) : array |
||
92 | |||
93 | private static function addRequestDataToHeaders(array $match, array $headers) : array |
||
99 | |||
100 | private static function addResponseDataToHeaders(array $match, array $headers) : array |
||
106 | |||
107 | /** |
||
108 | * Generate URL-encoded query string |
||
109 | * |
||
110 | * Example: |
||
111 | * <code> |
||
112 | * $parameters = [ |
||
113 | * 'param1' => ['value', 'another value'], |
||
114 | * 'param2' => 'a value', |
||
115 | * 'param3' => false, |
||
116 | * ]; |
||
117 | * |
||
118 | * $queryString = \TraderInteractive\HttpUtil::buildQueryString($parameters); |
||
119 | * |
||
120 | * echo $queryString |
||
121 | * </code> |
||
122 | * |
||
123 | * Output: |
||
124 | * <pre> |
||
125 | * param1=value¶m1=another+value¶m2=a+value¶m3=false |
||
126 | * </pre> |
||
127 | * |
||
128 | * @param array $parameters An associative array containing parameter key/value(s) |
||
129 | * |
||
130 | * @return string the built query string |
||
131 | */ |
||
132 | public static function buildQueryString(array $parameters) : string |
||
155 | |||
156 | /** |
||
157 | * Get an array of all url parameters. |
||
158 | * |
||
159 | * @param string $url The url to parse such as http://foo.com/bar/?id=boo&another=wee&another=boo |
||
160 | * @param array $collapsedParams Parameters to collapse. ex. 'id' => ['boo'] to just 'id' => 'boo'. Exception thrown |
||
161 | * if more than 1 value |
||
162 | * |
||
163 | * @return array such as ['id' => ['boo'], 'another' => ['wee', 'boo']] |
||
164 | * |
||
165 | * @throws Exception if more than one value in a $collapsedParams param |
||
166 | */ |
||
167 | public static function getQueryParams(string $url, array $collapsedParams = []) : array |
||
207 | |||
208 | /** |
||
209 | * Get an array of all url parameters. |
||
210 | * |
||
211 | * @param string $url The url to parse such as http://foo.com/bar/?single=boo&multi=wee&multi=boo |
||
212 | * @param array $expectedArrayParams List of parameter names which are not collapsed. |
||
213 | * |
||
214 | * @return array such as ['single' => 'boo', 'multi' => ['wee', 'boo']] if 'multi' is given in $expectedArrayParams |
||
215 | * |
||
216 | * @throws Exception if a parameter is given as array but not included in the expected array argument |
||
217 | */ |
||
218 | public static function getQueryParamsCollapsed(string $url, array $expectedArrayParams = []) : array |
||
255 | } |
||
256 |