Posted By

touv on 07/21/09


Tagged

http headers response


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

vali29


parse http response


 / Published in: PHP
 

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

  1. function parse_http_response ($string)
  2. {
  3.  
  4. $headers = array();
  5. $content = '';
  6. $str = strtok($string, "\n");
  7. $h = null;
  8. while ($str !== false) {
  9. if ($h and trim($str) === '') {
  10. $h = false;
  11. continue;
  12. }
  13. if ($h !== false and false !== strpos($str, ':')) {
  14. $h = true;
  15. list($headername, $headervalue) = explode(':', trim($str), 2);
  16. $headername = strtolower($headername);
  17. $headervalue = ltrim($headervalue);
  18. if (isset($headers[$headername]))
  19. $headers[$headername] .= ',' . $headervalue;
  20. else
  21. $headers[$headername] = $headervalue;
  22. }
  23. if ($h === false) {
  24. $content .= $str."\n";
  25. }
  26. $str = strtok("\n");
  27. }
  28. return array($headers, trim($content));
  29. }

Report this snippet  

You need to login to post a comment.