You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
4.8 KiB

  1. <?php
  2. /*
  3. This file is part of ActiveLink PHP NET Package (www.active-link.com).
  4. Copyright (c) 2002-2004 by Zurab Davitiani
  5. You can contact the author of this software via E-mail at
  6. hattrick@mailcan.com
  7. ActiveLink PHP NET Package is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as published by
  9. the Free Software Foundation; either version 2.1 of the License, or
  10. (at your option) any later version.
  11. ActiveLink PHP NET Package is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Lesser General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with ActiveLink PHP NET Package; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * requires Socket class
  21. */
  22. import("org.active-link.net.Socket");
  23. /**
  24. * HTTPClient class provides HTTP request functionality and ability to retrieve response
  25. * @class HTTPClient
  26. * @package org.active-link.net
  27. * @author Zurab Davitiani
  28. * @version 0.4.0
  29. * @extends Socket
  30. * @requires Socket
  31. * @see Socket
  32. */
  33. class HTTPClient extends Socket {
  34. // protected properties
  35. var $defaultRequestMethod;
  36. var $defaultRequestURI;
  37. var $defaultRequestVersion;
  38. var $defaultRequestUserAgent;
  39. var $defaultRequestBody;
  40. var $requestMethod;
  41. var $requestURI;
  42. var $requestVersion;
  43. var $requestUserAgent;
  44. var $requestHeaders;
  45. /**
  46. * HTTP client class constructor accepts host (required) and port (optional, default 80) arguments
  47. * @method HTTPClient
  48. * @param string host
  49. * @param optional int port
  50. */
  51. function HTTPClient($host, $port = 80) {
  52. $this->Socket($host, $port);
  53. $this->defaultRequestMethod = "GET";
  54. $this->defaultRequestURI = "/";
  55. $this->defaultRequestVersion = "HTTP/1.0";
  56. $this->defaultRequestUserAgent = "ActiveLink NET Object/0.3.3";
  57. $this->defaultRequestBody = "";
  58. $this->requestMethod = $this->defaultRequestMethod;
  59. $this->requestURI = $this->defaultRequestURI;
  60. $this->requestVersion = $this->defaultRequestVersion;
  61. $this->requestUserAgent = $this->defaultRequestUserAgent;
  62. $this->requestBody = $this->defaultRequestBody;
  63. $this->requestHeaders = array();
  64. }
  65. /**
  66. * Adds a supplied raw header to the internal header array
  67. * @method addRequestHeaderRaw
  68. * @param string header
  69. * @returns none
  70. */
  71. function addRequestHeaderRaw($header) {
  72. $this->requestHeaders[] = $header;
  73. }
  74. /**
  75. * Gets a string containing all HTTP request headers in their raw form
  76. * @method getRequestHeaders
  77. * @returns string request HTTP headers
  78. */
  79. function getRequestHeaders() {
  80. $headers = $this->requestMethod . " " . $this->requestURI . " " . $this->requestVersion . "\r\n";
  81. $headers .= "User-Agent: " . $this->requestUserAgent . "\r\n";
  82. $headers .= "Host: " . $this->host . "\r\n";
  83. foreach($this->requestHeaders as $header) {
  84. $headers .= $header . "\r\n";
  85. }
  86. if($this->requestMethod == "POST") {
  87. $contentLength = strlen($this->requestBody);
  88. $headers .= "Content-length: " . $contentLength . "\r\n";
  89. }
  90. $headers .= "Connection: close\r\n\r\n";
  91. return $headers;
  92. }
  93. /**
  94. * Sets HTTP request body/payload, used only when request method is POST
  95. * @method setRequestBody
  96. * @param string body
  97. * @returns none
  98. */
  99. function setRequestBody($body) {
  100. $this->requestBody = $body;
  101. }
  102. /**
  103. * Sets HTTP request method, GET or POST
  104. * @method setRequestMethod
  105. * @param string method
  106. * @returns none
  107. */
  108. function setRequestMethod($method) {
  109. $this->requestMethod = strtoupper($method);
  110. }
  111. /**
  112. * Sets request URI, if not set here, default will be /
  113. * @method setRequestURI
  114. * @param string uri
  115. * @returns none
  116. */
  117. function setRequestURI($uri) {
  118. $this->requestURI = $uri;
  119. }
  120. /**
  121. * Sets HTTP request User-Agent to send to the server, default is "ActiveLink NET Object/version"
  122. * @method setRequestUserAgent
  123. * @param string userAgent
  124. * @returns none
  125. */
  126. function setRequestUserAgent($userAgent) {
  127. $this->setRequestUserAgent = $userAgent;
  128. }
  129. /**
  130. * Sets HTTP protocol version to be used, default is "HTTP/1.0"
  131. * @method setRequestVersion
  132. * @param string version
  133. * @returns none
  134. */
  135. function setRequestVersion($version) {
  136. $this->requestVersion = $version;
  137. }
  138. /**
  139. * After all settings are complete, send the request to the server
  140. * @method sendRequest
  141. * @returns string server response if successful, false otherwise
  142. */
  143. function sendRequest() {
  144. $response = false;
  145. $request = $this->getRequestHeaders();
  146. $request .= $this->requestBody;
  147. $success = $this->connect();
  148. if($success) {
  149. $response = $this->sendReceive($request);
  150. $this->disconnect();
  151. }
  152. return $response;
  153. }
  154. }