httpClient Class Reference

List of all members.

Public Member Functions

  • httpClient ($host= '', $port= '')
    httpClient constructor Note: when host and port are defined, the connection is immediate connect
  • setProxy ($proxyHost, $proxyPort)
    turn on proxy support
  • setProtocolVersion ($version)
    setProtocolVersion define the HTTP protocol version to use
  • setCredentials ($username, $password)
    set a username and password to access a protected resource Only "Basic" authentication scheme is supported yet
  • setHeaders ($headers)
    define a set of HTTP headers to be sent to the server header names are lowercased to avoid duplicated headers
  • addHeader ($headerName, $headerValue)
    addHeader set a unique request header
  • removeHeader ($headerName)
    removeHeader unset a request header
  • Connect ($host, $port= '')
    Connect open the connection to the server.
  • Disconnect ()
    Disconnect close the connection to the server.
  • Head ($uri)
    head issue a HEAD request
  • Get ($url)
    get issue a GET http request
  • Post ($uri, $query_params= '')
  • Put ($uri, $filecontent)
    Put Send a PUT request PUT is the method to sending a file on the server.
  • getHeaders ()
    getHeaders return the response headers to be called after a Get() or Head() call
  • getHeader ($headername)
    getHeader return the response header "headername"
  • getBody ()
    getBody return the response body invoke it after a Get() call for instance, to retrieve the response
  • getStatus ()
    getStatus return the server response's status code
  • getStatusMessage ()
    getStatusMessage return the full response status, of the form "CODE Message" eg.
  • sendCommand ($command)
    only protected or private methods below
  • processReply ()
  • processHeader ($lastLine="\r\n")
    processHeader() reads header lines from socket until the line equals $lastLine protected
  • processBody ()
    processBody() reads the body from the socket the body is the "real" content of the reply
  • makeUri ($uri)
    Calculate and return the URI to be sent ( proxy purpose ).

Public Attributes


Detailed Description

Definition at line 39 of file http_client.php.


Member Function Documentation

httpClient::addHeader ( headerName,
headerValue 
)

addHeader set a unique request header

Parameters:
headerName the header name
headerValue the header value, ( unencoded)

Definition at line 118 of file http_client.php.

Referenced by Post(), sendCommand(), and setCredentials().

00118                                                   {
00119       $this->requestHeaders[$headerName] = $headerValue;
00120     }

httpClient::Connect ( host,
port = '' 
)

Connect open the connection to the server.

Parameters:
host string server address (or IP)
port string server listening port - defaults to 80
Returns:
boolean false is connection failed, true otherwise

Definition at line 138 of file http_client.php.

00138                                         {
00139       $this->url['scheme'] = 'http';
00140       $this->url['host'] = $host;
00141       if (tep_not_null($port)) $this->url['port'] = $port;
00142 
00143       return true;
00144     }

httpClient::Disconnect (  ) 

Disconnect close the connection to the server.

Definition at line 150 of file http_client.php.

00150                           {
00151       if ($this->socket) fclose($this->socket);
00152     }

httpClient::Get ( url  ) 

get issue a GET http request

Parameters:
uri URI (path on server) or full URL of the document
Returns:
string response status code (200 if ok) getHeaders(), getBody()

Definition at line 180 of file http_client.php.

00180                        {
00181       $this->responseHeaders = $this->responseBody = '';
00182 
00183       $uri = $this->makeUri($url);
00184 
00185       if ($this->sendCommand('GET ' . $uri . ' HTTP/' . $this->protocolVersion)) {
00186         $this->processReply();
00187       }
00188 
00189       return $this->reply;
00190     }

httpClient::getBody (  ) 

getBody return the response body invoke it after a Get() call for instance, to retrieve the response

Returns:
string body content get, head

Definition at line 277 of file http_client.php.

00277                        {
00278       return $this->responseBody;
00279     }

httpClient::getHeader ( headername  ) 

getHeader return the response header "headername"

Parameters:
headername the name of the header
Returns:
header value or NULL if no such header is defined

Definition at line 266 of file http_client.php.

00266                                     {
00267       return $this->responseHeaders[$headername];
00268     }

httpClient::getHeaders (  ) 

getHeaders return the response headers to be called after a Get() or Head() call

Returns:
array headers received from server in the form headername => value get, head

Definition at line 256 of file http_client.php.

00256                           {
00257       return $this->responseHeaders;
00258     }

httpClient::getStatus (  ) 

getStatus return the server response's status code

Returns:
string a status code code are divided in classes (where x is a digit)
  • 20x : request processed OK
  • 30x : document moved
  • 40x : client error ( bad url, document not found, etc...)
  • 50x : server error
See also:
RFC2616 "Hypertext Transfer Protocol -- HTTP/1.1"

Definition at line 291 of file http_client.php.

00291                          {
00292       return $this->reply;
00293     }

httpClient::getStatusMessage (  ) 

getStatusMessage return the full response status, of the form "CODE Message" eg.

"404 Document not found"

Returns:
string the message

Definition at line 300 of file http_client.php.

00300                                 {
00301       return $this->replyString;
00302     }

httpClient::Head ( uri  ) 

head issue a HEAD request

Parameters:
uri string URI of the document
Returns:
string response status code (200 if ok) getHeaders()

Definition at line 161 of file http_client.php.

00161                         {
00162       $this->responseHeaders = $this->responseBody = '';
00163 
00164       $uri = $this->makeUri($uri);
00165 
00166       if ($this->sendCommand('HEAD ' . $uri . ' HTTP/' . $this->protocolVersion)) {
00167         $this->processReply();
00168       }
00169 
00170       return $this->reply;
00171     }

httpClient::httpClient ( host = '',
port = '' 
)

httpClient constructor Note: when host and port are defined, the connection is immediate connect

Definition at line 55 of file http_client.php.

00055                                                 {
00056       if (tep_not_null($host)) {
00057         $this->connect($host, $port);
00058       }
00059     }

httpClient::makeUri ( uri  ) 

Calculate and return the URI to be sent ( proxy purpose ).

Parameters:
the local URI
Returns:
URI to be used in the HTTP request private

Definition at line 441 of file http_client.php.

Referenced by Get(), Head(), Post(), and Put().

00441                            {
00442       $a = parse_url($uri);
00443 
00444       if ( (isset($a['scheme'])) && (isset($a['host'])) ) {
00445         $this->url = $a;
00446       } else {
00447         unset($this->url['query']);
00448         unset($this->url['fragment']);
00449         $this->url = array_merge($this->url, $a);
00450       }
00451 
00452       if ($this->useProxy) {
00453         $requesturi = 'http://' . $this->url['host'] . (empty($this->url['port']) ? '' : ':' . $this->url['port']) . $this->url['path'] . (empty($this->url['query']) ? '' : '?' . $this->url['query']);
00454       } else {
00455         $requesturi = $this->url['path'] . (empty($this->url['query']) ? '' : '?' . $this->url['query']);
00456       }
00457 
00458       return $requesturi;
00459     }

httpClient::Post ( uri,
query_params = '' 
)

Definition at line 202 of file http_client.php.

00202                                             {
00203       $uri = $this->makeUri($uri);
00204 
00205       if (is_array($query_params)) {
00206         $postArray = array();
00207         reset($query_params);
00208         while (list($k, $v) = each($query_params)) {
00209           $postArray[] = urlencode($k) . '=' . urlencode($v);
00210         }
00211 
00212         $this->requestBody = implode('&', $postArray);
00213       }
00214 
00215 // set the content type for post parameters
00216       $this->addHeader('Content-Type', 'application/x-www-form-urlencoded');
00217 
00218       if ($this->sendCommand('POST ' . $uri . ' HTTP/' . $this->protocolVersion)) {
00219         $this->processReply();
00220       }
00221 
00222       $this->removeHeader('Content-Type');
00223       $this->removeHeader('Content-Length');
00224       $this->requestBody = '';
00225 
00226       return $this->reply;
00227     }

httpClient::processBody (  ) 

processBody() reads the body from the socket the body is the "real" content of the reply

Returns:
string body content private

Definition at line 410 of file http_client.php.

Referenced by processReply().

00410                            {
00411       $data = '';
00412       $counter = 0;
00413 
00414       do {
00415         $status = socket_get_status($this->socket);
00416         if ($status['eof'] == 1) {
00417           break;
00418         }
00419 
00420         if ($status['unread_bytes'] > 0) {
00421           $buffer = fread($this->socket, $status['unread_bytes']);
00422           $counter = 0;
00423         } else {
00424           $buffer = fread($this->socket, 128);
00425           $counter++;
00426           usleep(2);
00427         }
00428 
00429         $data .= $buffer;
00430       } while ( ($status['unread_bytes'] > 0) || ($counter++ < 10) );
00431 
00432       return $data;
00433     }

httpClient::processHeader ( lastLine = "\r\n"  ) 

processHeader() reads header lines from socket until the line equals $lastLine protected

Returns:
array of headers with header names as keys and header content as values

Definition at line 383 of file http_client.php.

Referenced by processReply().

00383                                                {
00384       $headers = array();
00385       $finished = false;
00386 
00387       while ( (!$finished) && (!feof($this->socket)) ) {
00388         $str = fgets($this->socket, 1024);
00389         $finished = ($str == $lastLine);
00390         if (!$finished) {
00391           list($hdr, $value) = preg_split('/: /', $str, 2);
00392 // nasty workaround broken multiple same headers (eg. Set-Cookie headers) @FIXME
00393           if (isset($headers[$hdr])) {
00394             $headers[$hdr] .= '; ' . trim($value);
00395           } else {
00396             $headers[$hdr] = trim($value);
00397           }
00398         }
00399       }
00400 
00401       return $headers;
00402     }

httpClient::processReply (  ) 

Definition at line 362 of file http_client.php.

Referenced by Get(), Head(), Post(), and Put().

00362                             {
00363       $this->replyString = trim(fgets($this->socket, 1024));
00364 
00365       if (preg_match('|^HTTP/\S+ (\d+) |i', $this->replyString, $a )) {
00366         $this->reply = $a[1];
00367       } else {
00368         $this->reply = 'Bad Response';
00369       }
00370 
00371 //get response headers and body
00372       $this->responseHeaders = $this->processHeader();
00373       $this->responseBody = $this->processBody();
00374 
00375       return $this->reply;
00376     }

httpClient::Put ( uri,
filecontent 
)

Put Send a PUT request PUT is the method to sending a file on the server.

it is *not* widely supported

Parameters:
uri the location of the file on the server. dont forget the heading "/"
filecontent the content of the file. binary content accepted
Returns:
string response status code 201 (Created) if ok
See also:
RFC2518 "HTTP Extensions for Distributed Authoring WEBDAV"

Definition at line 238 of file http_client.php.

00238                                      {
00239       $uri = $this->makeUri($uri);
00240       $this->requestBody = $filecontent;
00241 
00242       if ($this->sendCommand('PUT ' . $uri . ' HTTP/' . $this->protocolVersion)) {
00243         $this->processReply();
00244       }
00245 
00246       return $this->reply;
00247     }

httpClient::removeHeader ( headerName  ) 

removeHeader unset a request header

Parameters:
headerName the header name

Definition at line 127 of file http_client.php.

Referenced by Post().

00127                                        {
00128       unset($this->requestHeaders[$headerName]);
00129     }

httpClient::sendCommand ( command  ) 

only protected or private methods below

send a request data sent are in order a) the command b) the request headers if they are defined c) the request body if defined

Returns:
string the server repsonse status code

Definition at line 316 of file http_client.php.

Referenced by Get(), Head(), Post(), and Put().

00316                                    {
00317       $this->responseHeaders = array();
00318       $this->responseBody = '';
00319 
00320 // connect if necessary
00321       if ( ($this->socket == false) || (feof($this->socket)) ) {
00322         if ($this->useProxy) {
00323           $host = $this->proxyHost;
00324           $port = $this->proxyPort;
00325         } else {
00326           $host = $this->url['host'];
00327           $port = $this->url['port'];
00328         }
00329 
00330         if (!tep_not_null($port)) $port = 80;
00331 
00332         if (!$this->socket = fsockopen($host, $port, $this->reply, $this->replyString)) {
00333           return false;
00334         }
00335 
00336         if (tep_not_null($this->requestBody)) {
00337           $this->addHeader('Content-Length', strlen($this->requestBody));
00338         }
00339 
00340         $this->request = $command;
00341         $cmd = $command . "\r\n";
00342         if (is_array($this->requestHeaders)) {
00343           reset($this->requestHeaders);
00344           while (list($k, $v) = each($this->requestHeaders)) {
00345             $cmd .= $k . ': ' . $v . "\r\n";
00346           }
00347         }
00348 
00349         if (tep_not_null($this->requestBody)) {
00350           $cmd .= "\r\n" . $this->requestBody;
00351         }
00352 
00353 // unset body (in case of successive requests)
00354         $this->requestBody = '';
00355 
00356         fputs($this->socket, $cmd . "\r\n");
00357 
00358         return true;
00359       }
00360     }

httpClient::setCredentials ( username,
password 
)

set a username and password to access a protected resource Only "Basic" authentication scheme is supported yet

Parameters:
username string - identifier
password string - clear password

Definition at line 94 of file http_client.php.

00094                                                   {
00095       $this->addHeader('Authorization', 'Basic ' . base64_encode($username . ':' . $password));
00096      }

httpClient::setHeaders ( headers  ) 

define a set of HTTP headers to be sent to the server header names are lowercased to avoid duplicated headers

Parameters:
headers hash array containing the headers as headerName => headerValue pairs

Definition at line 103 of file http_client.php.

00103                                   {
00104       if (is_array($headers)) {
00105         reset($headers);
00106         while (list($name, $value) = each($headers)) {
00107           $this->requestHeaders[$name] = $value;
00108         }
00109       }
00110     }

httpClient::setProtocolVersion ( version  ) 

setProtocolVersion define the HTTP protocol version to use

Parameters:
version string the version number with one decimal: "0.9", "1.0", "1.1" when using 1.1, you MUST set the mandatory headers "Host"
Returns:
boolean false if the version number is bad, true if ok

Definition at line 79 of file http_client.php.

00079                                           {
00080       if ( ($version > 0) && ($version <= 1.1) ) {
00081         $this->protocolVersion = $version;
00082         return true;
00083       } else {
00084         return false;
00085       }
00086     }

httpClient::setProxy ( proxyHost,
proxyPort 
)

turn on proxy support

Parameters:
proxyHost proxy host address eg "proxy.mycorp.com"
proxyPort proxy port usually 80 or 8080

Definition at line 66 of file http_client.php.

00066                                               {
00067       $this->useProxy = true;
00068       $this->proxyHost = $proxyHost;
00069       $this->proxyPort = $proxyPort;
00070     }


Member Data Documentation

httpClient::$protocolVersion = '1.1'

Definition at line 43 of file http_client.php.

httpClient::$proxyHost

Definition at line 48 of file http_client.php.

Referenced by setProxy().

httpClient::$proxyPort

Definition at line 48 of file http_client.php.

Referenced by setProxy().

httpClient::$reply

Definition at line 41 of file http_client.php.

httpClient::$replyString

Definition at line 42 of file http_client.php.

httpClient::$requestBody

Definition at line 44 of file http_client.php.

httpClient::$requestHeaders

Definition at line 44 of file http_client.php.

httpClient::$socket = false

Definition at line 45 of file http_client.php.

httpClient::$url

Definition at line 40 of file http_client.php.

Referenced by Get().

httpClient::$useProxy = false

Definition at line 47 of file http_client.php.


The documentation for this class was generated from the following file: