User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 373,573 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,872 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Dec 18th, 2006
Views: 12,690
Searching the net I couldn't find a simple way of retrieving HTTP Request Headers and HTTP Request Body in a PHP Script.

PECL extensions offer this but not a standard PHP install.

So heres a simple class that will do it for you.

Docs in my blog: http://fijiwebdesign.com/content/view/90/77/
Last edited : Dec 26th, 2006.
php Syntax | 4 stars
  1. <?php
  2.  
  3. /**
  4. * Access the HTTP Request
  5. */
  6. class http_request {
  7.  
  8. /** additional HTTP headers not prefixed with HTTP_ in $_SERVER superglobal */
  9. var $add_headers = array('CONTENT_TYPE', 'CONTENT_LENGTH');
  10.  
  11. /**
  12. * Construtor
  13. * Retrieve HTTP Body
  14. * @param Array Additional Headers to retrieve
  15. */
  16. function http_request($add_headers = false) {
  17.  
  18. $this->retrieve_headers($add_headers);
  19. $this->body = @file_get_contents('php://input');
  20. }
  21.  
  22. /**
  23. * Retrieve the HTTP request headers from the $_SERVER superglobal
  24. * @param Array Additional Headers to retrieve
  25. */
  26. function retrieve_headers($add_headers = false) {
  27.  
  28. if ($add_headers) {
  29. $this->add_headers = array_merge($this->add_headers, $add_headers);
  30. }
  31.  
  32. if (isset($_SERVER['HTTP_METHOD'])) {
  33. $this->method = $_SERVER['HTTP_METHOD'];
  34. unset($_SERVER['HTTP_METHOD']);
  35. } else {
  36. $this->method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;
  37. }
  38. $this->protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : false;
  39. $this->request_method = isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : false;
  40.  
  41. $this->headers = array();
  42. foreach($_SERVER as $i=>$val) {
  43. if (strpos($i, 'HTTP_') === 0 || in_array($i, $this->add_headers)) {
  44. $name = str_replace(array('HTTP_', '_'), array('', '-'), $i);
  45. $this->headers[$name] = $val;
  46. }
  47. }
  48. }
  49.  
  50. /**
  51. * Retrieve HTTP Method
  52. */
  53. function method() {
  54. return $this->method;
  55. }
  56.  
  57. /**
  58. * Retrieve HTTP Body
  59. */
  60. function body() {
  61. return $this->body;
  62. }
  63.  
  64. /**
  65. * Retrieve an HTTP Header
  66. * @param string Case-Insensitive HTTP Header Name (eg: "User-Agent")
  67. */
  68. function header($name) {
  69. $name = strtoupper($name);
  70. return isset($this->headers[$name]) ? $this->headers[$name] : false;
  71. }
  72.  
  73. /**
  74. * Retrieve all HTTP Headers
  75. * @return array HTTP Headers
  76. */
  77. function headers() {
  78. return $this->headers;
  79. }
  80.  
  81. /**
  82. * Return Raw HTTP Request (note: This is incomplete)
  83. * @param bool ReBuild the Raw HTTP Request
  84. */
  85. function raw($refresh = false) {
  86.  
  87. if (isset($this->raw) && !$refresh) {
  88. return $this->raw; // return cached
  89. }
  90.  
  91. $headers = $this->headers();
  92. $this->raw = "{$this->method}\r\n";
  93.  
  94. foreach($headers as $i=>$header) {
  95. $this->raw .= "$i: $header\r\n";
  96. }
  97.  
  98. $this->raw .= "\r\n{$http_request->body}";
  99.  
  100. return $this->raw;
  101. }
  102.  
  103. }
  104.  
  105. /**
  106. * Example Usage
  107. * Echos the HTTP Request back to the client/browser
  108. */
  109.  
  110. $http_request = new http_request();
  111.  
  112. $resp = $http_request->raw();
  113. echo nl2br($resp);
  114.  
  115. ?>
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 7:44 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC