src/Util/Http.php
public static function parseHeaders(string $rawHeaders) : array
    {
        if (empty(trim($rawHeaders))) {
            throw new InvalidArgumentException('$rawHeaders cannot be whitespace');
        }

        $headers = [];
        $rawHeaders = preg_replace("/\r\n[\t ]+/", ' ', trim($rawHeaders));
        $fields = explode("\r\n", $rawHeaders);
        foreach ($fields as $field) {
            $match = null;
            if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
                $key = $match[1];
                // convert 'some-header' to 'Some-Header'
                $key = strtolower(trim($key));
                $key = ucwords(preg_replace('/[\s-]/', ' ', $key));
                $key = strtr($key, ' ', '-');

                $value = trim($match[2]);

                if (!array_key_exists($key, $headers)) {
                    $headers[$key] = $value;
                    continue;
                }

                if (!is_array($headers[$key])) {
                    $headers[$key] = [$headers[$key]];
                }

                $headers[$key][] = $value;
                continue;
            }

            if (preg_match('#([A-Za-z]+) +([^ ]+) +HTTP/([\d.]+)#', $field, $match)) {
                $headers = self::addRequestDataToHeaders($match, $headers);
                continue;
            }

            if (preg_match('#HTTP/([\d.]+) +(\d{3}) +(.*)#', $field, $match)) {
                $headers = self::addResponseDataToHeaders($match, $headers);
                continue;
            }

            throw new Exception("Unsupported header format: {$field}");
        }

        return $headers;
    }
public static function parseHeaders(string $rawHeaders) : array
    {
        if (empty(trim($rawHeaders))) {
            throw new InvalidArgumentException('$rawHeaders cannot be whitespace');
        }

        $headers = [];
        $rawHeaders = preg_replace("/\r\n[\t ]+/", ' ', trim($rawHeaders));
        $fields = explode("\r\n", $rawHeaders);
        foreach ($fields as $field) {
            $match = null;
            if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
                $key = $match[1];
                // convert 'some-header' to 'Some-Header'
                $key = strtolower(trim($key));
                $key = ucwords(preg_replace('/[\s-]/', ' ', $key));
                $key = strtr($key, ' ', '-');

                $value = trim($match[2]);

                if (!array_key_exists($key, $headers)) {
                    $headers[$key] = $value;
                    continue;
                }

                if (!is_array($headers[$key])) {
                    $headers[$key] = [$headers[$key]];
                }

                $headers[$key][] = $value;
                continue;
            }

            if (preg_match('#([A-Za-z]+) +([^ ]+) +HTTP/([\d.]+)#', $field, $match)) {
                $headers = self::addRequestDataToHeaders($match, $headers);
                continue;
            }

            if (preg_match('#HTTP/([\d.]+) +(\d{3}) +(.*)#', $field, $match)) {
                $headers = self::addResponseDataToHeaders($match, $headers);
                continue;
            }

            throw new Exception("Unsupported header format: {$field}");
        }

        return $headers;
    }
public static function parseHeaders ( string $ rawHeaders ) : array { if ( empty ( trim ( $ rawHeaders ) ) ) { throw new InvalidArgumentException ( '$rawHeaders cannot be whitespace' ) ; } $ headers = [ ] ; $ rawHeaders = preg_replace ( "/\r\n[\t ]+/" , ' ' , trim ( $ rawHeaders ) ) ; $ fields = explode ( "\r\n" , $ rawHeaders ) ; foreach ( $ fields as $ field ) { $ match = null ; if ( preg_match ( '/([^:]+): (.+)/m' , $ field , $ match ) ) { $ key = $ match [ 1 ] ; // convert 'some-header' to 'Some-Header' $ key = strtolower ( trim ( $ key ) ) ; $ key = ucwords ( preg_replace ( '/[\s-]/' , ' ' , $ key ) ) ; $ key = strtr ( $ key , ' ' , '-' ) ; $ value = trim ( $ match [ 2 ] ) ; if ( ! array_key_exists ( $ key , $ headers ) ) { $ headers [ $ key ] = $ value ; continue ; } if ( ! is_array ( $ headers [ $ key ] ) ) { $ headers [ $ key ] = [ $ headers [ $ key ] ] ; } $ headers [ $ key ] [ ] = $ value ; continue ; } if ( preg_match ( '#([A-Za-z]+) +([^ ]+) +HTTP/([\d.]+)#' , $ field , $ match ) ) { $ headers = self :: addRequestDataToHeaders ( $ match , $ headers ) ; continue ; } if ( preg_match ( '#HTTP/([\d.]+) +(\d{3}) +(.*)#' , $ field , $ match ) ) { $ headers = self :: addResponseDataToHeaders ( $ match , $ headers ) ; continue ; } throw new Exception ( "Unsupported header format: {$field}" ) ; } return $ headers ; }
Parses HTTP headers into an associative array.

Example:
<code>
$headers = "HTTP/1.1 200 OK\r\n".
"content-type: text/html; charset=UTF-8\r\n".
"Server: Funky/1.0\r\n".
"Set-Cookie: foo=bar\r\n".
"Set-Cookie: baz=quux\r\n".
"Folds: are\r\n\treformatted\r\n";
print_r(\TraderInteractive\HttpUtil::parseHeaders($headers));
</code>
The above example will output:
<pre>
Array
(
[Response Code] => 200
[Response Status] => OK
[Content-Type] => text/html; charset=UTF-8
[Server] => Funky/1.0
[Set-Cookie] => Array
(
[0] => foo=bar
[1] => baz=quux
)
[Folds] => are reformatted
)
</pre>

@param string $rawHeaders string containing HTTP headers

@return array the parsed headers

@throws Exception Thrown if unable to parse the headers
Parses HTTP headers into an associative array .