parse http response


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.