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