Return to Snippet

Revision: 15911
at July 29, 2009 05:49 by touv


Updated Code
function parse_http_response ($string) 
    {

        $headers = array();
        $content = '';
        $str = strtok($string, "\n");
        $h = null;
        while ($str !== false) {
            if ($h and trim($str) === '') {                
                $h = false;
                continue;
            }
            if ($h !== false and false !== strpos($str, ':')) {
                $h = true;
                list($headername, $headervalue) = explode(':', trim($str), 2);
                $headername = strtolower($headername);
                $headervalue = ltrim($headervalue);
                if (isset($headers[$headername])) 
                    $headers[$headername] .= ',' . $headervalue;
                else 
                    $headers[$headername] = $headervalue;
            }
            if ($h === false) {
                $content .= $str."\n";
            }
            $str = strtok("\n");
        }
        return array($headers, trim($content));
    }

Revision: 15910
at July 21, 2009 03:45 by touv


Updated Code
function parse_http_headers($string) {
    
    $headers = array();
    $str = strtok($string, "\n");
    while ($str !== false) {
        if (false !== strpos($str, ':')) {
            list($headername, $headervalue) = explode(':', trim($str), 2);
            $headername = strtolower($headername);
            $headervalue = ltrim($headervalue);
            if (isset($headers[$headername])) 
                $headers[$headername] .= ',' . $headervalue;
            else 
                $headers[$headername] = $headervalue;
        }
        $str = strtok("\n");
    }
    return $headers;
}

Revision: 15909
at July 21, 2009 03:44 by touv


Initial Code
function parse_http_header($string) {
    
    $headers = array();
    $str = strtok($string, "\n");
    while ($str !== false) {
        if (false !== strpos($str, ':')) {
            list($headername, $headervalue) = explode(':', trim($str), 2);
            $headername = strtolower($headername);
            $headervalue = ltrim($headervalue);
            if (isset($headers[$headername])) 
                $headers[$headername] .= ',' . $headervalue;
            else 
                $headers[$headername] = $headervalue;
        }
        $str = strtok("\n");
    }
    return $headers;
}

Initial URL

                                

Initial Description
Split a string containing HTTP response into  a array with HTTP headers and a string with HTTP content

Initial Title
parse http response

Initial Tags
http

Initial Language
PHP