HPCloud-PHP  1.2.0
PHP bindings for HPCloud and OpenStack services.
 All Classes Namespaces Files Functions Variables Pages
PHPStreamTransport Class Reference

Provide HTTP transport with the PHP HTTP stream wrapper. More...

Inheritance diagram for PHPStreamTransport:
Transporter

Public Member Functions

 doRequest ($uri, $method= 'GET', $headers=array(), $body= '')
 Perform a request.
 doRequestWithResource ($uri, $method, $headers, $resource)
 Implements Transporter::doRequestWithResource().
 onNotification (callable $callable)
 Register an event handler for notifications.
 printNotifications ($code, $severity, $msg, $msgcode, $bytes, $len)

Public Attributes

const HTTP_USER_AGENT_SUFFIX = ' (b2d770) PHP/1.0'
 $httpVersion = '1.1'
 The HTTP version this should use.
- Public Attributes inherited from Transporter
const HTTP_USER_AGENT = 'HPCloud-PHP/1.0'

Protected Member Functions

 guessError ($err, $uri, $method)
 Given an error, this tries to guess the cause and throw an exception.
 smashHeaders ($headers)
 Given an array of headers, build a header string.
 buildStreamContext ($method, $headers, $body)
 Build the stream context for a request.

Protected Attributes

 $notificationCallback = NULL
 The event watcher callback.

Detailed Description

Provide HTTP transport with the PHP HTTP stream wrapper.

PHP comes with a stream wrapper for HTTP. Actually, it comes with two such stream wrappers, and the compile-time options determine which is used. This transporter uses the stream wrapper library to send requests to the remote host.

Several properties are declared public, and can be changed to suite your needs.

You can use a single PHPStreamTransport object to execute multiple requests.

Attention
This class should not be constructed directly. Use HPCloud::Transport::instance() to get an instance.

See HPCloud::Transport and HPCloud::Bootstrap.

Definition at line 47 of file PHPStreamTransport.php.

Member Function Documentation

buildStreamContext (   $method,
  $headers,
  $body 
)
protected

Build the stream context for a request.

All of the HTTP transport data is passed into PHP's stream wrapper via a stream context. This builds the context.

Definition at line 235 of file PHPStreamTransport.php.

References Bootstrap\config(), Bootstrap\hasConfig(), and PHPStreamTransport\smashHeaders().

Referenced by PHPStreamTransport\doRequest(), and PHPStreamTransport\doRequestWithResource().

doRequest (   $uri,
  $method = 'GET',
  $headers = array(),
  $body = '' 
)

Perform a request.

Invoking this method causes a single request to be relayed over the transporter. The transporter MUST be capable of handling multiple invocations of a doRequest() call.

Parameters
string$uriThe target URI.
string$methodThe method to be sent.
array$headersAn array of name/value header pairs.
string$bodyThe string containing the request body.

Implements Transporter.

Definition at line 67 of file PHPStreamTransport.php.

References $cxt, PHPStreamTransport\buildStreamContext(), PHPStreamTransport\guessError(), Bootstrap\hasConfig(), and PHP_EOL.

doRequestWithResource (   $uri,
  $method,
  $headers,
  $resource 
)

Implements Transporter::doRequestWithResource().

Unfortunately, PHP Stream Wrappers do not allow HTTP data to be read out of a file resource, so using this method will allow some performance improvement (because grabage collection can collect faster), but not a lot.

While PHP's underlying architecture should still adequately buffer large strings, the effects of this buffering on really large data (5G or so) is unknown.

Implements Transporter.

Definition at line 120 of file PHPStreamTransport.php.

References $cxt, PHPStreamTransport\buildStreamContext(), and PHPStreamTransport\guessError().

guessError (   $err,
  $uri,
  $method 
)
protected

Given an error, this tries to guess the cause and throw an exception.

Stream wrappers do not deal with error conditions gracefully. (For starters, during an error one cannot access the HTTP headers). The only useful piece of data given is the contents of the last error buffer.

This uses the contents of that buffer to attempt to learn what happened during the request. It then throws an exception that seems appropriate for the given context.

Definition at line 173 of file PHPStreamTransport.php.

Referenced by PHPStreamTransport\doRequest(), and PHPStreamTransport\doRequestWithResource().

onNotification ( callable  $callable)

Register an event handler for notifications.

During the course of a transaction, the stream wrapper emits a variety of notifications. This function can be used to register an event handler to listen for notifications.

Parameters
callable$callableAny callable, including an anonymous function or closure.
See Also
http://us3.php.net/manual/en/function.stream-notification-callback.php

Definition at line 197 of file PHPStreamTransport.php.

printNotifications (   $code,
  $severity,
  $msg,
  $msgcode,
  $bytes,
  $len 
)

Definition at line 270 of file PHPStreamTransport.php.

References PHP_EOL.

smashHeaders (   $headers)
protected

Given an array of headers, build a header string.

This builds an HTTP header string in the form required by the HTTP stream wrapper for PHP.

Parameters
array$headersAn associative array of header names to header values.
Return values
string
Returns
string A string containing formatted headers.

Definition at line 213 of file PHPStreamTransport.php.

References $name.

Referenced by PHPStreamTransport\buildStreamContext().

Member Data Documentation

$httpVersion = '1.1'

The HTTP version this should use.

By default, this is set to 1.1, which is not PHP's default. We do this to take advantage of chunked encoding. While this requires PHP 5.3.0 or greater, this is not viewed as a problem, given that the entire library requires PHP 5.3.

Definition at line 59 of file PHPStreamTransport.php.

$notificationCallback = NULL
protected

The event watcher callback.

Definition at line 65 of file PHPStreamTransport.php.

const HTTP_USER_AGENT_SUFFIX = ' (b2d770) PHP/1.0'

Definition at line 49 of file PHPStreamTransport.php.


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