class Body implements Serializable (View source)

The message body, represented as a PHP (temporary) stream.

NOTE: Currently, http\Message\Body::addForm() creates multipart/form-data bodies.

Methods

__construct(resource $stream = null)

Create a new message body, optionally referencing $stream.

string
__toString()

String cast handler.

addForm(array $fields = null, array $files = null)

Add form fields and files to the message body.

addPart(Message $part)

Add a part to a multipart body.

append(string $data)

Append plain bytes to the message body.

string|false
etag()

Retrieve the ETag of the body.

string|null
getBoundary()

Retrieve any boundary of the message body.

resource
getResource()

Retrieve the underlying stream resource.

string|null
serialize()

Implements Serializable.

int|object
stat(string $field = null)

Stat size, atime, mtime and/or ctime.

toCallback(callable $callback, int $offset = 0, int $maxlen = 0)

Stream the message body through a callback.

toStream(resource $stream, int $offset = 0, int $maxlen = 0)

Stream the message body into another stream $stream, starting from $offset, streaming $maxlen at most.

string
toString()

Retrieve the message body serialized to a string.

void
unserialize(string $serialized)

Implements Serializable.

Details

__construct(resource $stream = null)

Create a new message body, optionally referencing $stream.

Parameters

resource $stream

A stream to be used as message body.

Exceptions

InvalidArgumentException
UnexpectedValueException

string __toString()

String cast handler.

Return Value

string

the message body.

Body addForm(array $fields = null, array $files = null)

Add form fields and files to the message body.

NOTE: Currently, http\Message\Body::addForm() creates "multipart/form-data" bodies.

Parameters

array $fields

List of form fields to add.

array $files

List of form files to add.

$fields must look like:

[
  "field_name" => "value",
  "multi_field" => [
    "value1",
    "value2"
  ]
]

$files must look like:

[
  [
    "name" => "field_name",
    "type" => "content/type",
    "file" => "/path/to/file.ext"
  ], [
    "name" => "field_name2",
    "type" => "text/plain",
    "file" => "file.ext",
    "data" => "string"
  ], [
    "name" => "field_name3",
    "type" => "image/jpeg",
    "file" => "file.ext",
    "data" => fopen("/home/mike/Pictures/mike.jpg","r")
]

As you can see, a file structure must contain a "file" entry, which holds a file path, and an optional "data" entry, which may either contain a resource to read from or the actual data as string.

Return Value

Body self.

Exceptions

InvalidArgumentException
RuntimeException

Body addPart(Message $part)

Add a part to a multipart body.

Parameters

Message $part

The message part.

Return Value

Body self.

Exceptions

InvalidArgumentException
RuntimeException

Body append(string $data)

Append plain bytes to the message body.

Parameters

string $data

The data to append to the body.

Return Value

Body self.

Exceptions

InvalidArgumentException
RuntimeException

string|false etag()

Retrieve the ETag of the body.

Return Value

string|false

string an Apache style ETag of inode, mtime and size in hex concatenated by hyphens if the message body stream is stat-able. or string a content hash (which algorithm is determined by INI http.etag.mode) if the stream is not stat-able. or false if http.etag.mode is not a known hash algorithm.

string|null getBoundary()

Retrieve any boundary of the message body.

See http\Message::splitMultipartBody().

Return Value

string|null

string the message body boundary. or NULL if this message body has no boundary.

resource getResource()

Retrieve the underlying stream resource.

Return Value

resource

the underlying stream.

string|null serialize()

Implements Serializable.

Alias of http\Message\Body::__toString().

Return Value

string|null

The string representation of the object or null

int|object stat(string $field = null)

Stat size, atime, mtime and/or ctime.

Parameters

string $field

A single stat field to retrieve.

Return Value

int|object

int the requested stat field. or object stdClass instance holding all four stat fields.

Body toCallback(callable $callback, int $offset = 0, int $maxlen = 0)

Stream the message body through a callback.

Parameters

callable $callback

The callback of the form function(http\Message\Body $from, string $data).

int $offset

Start to stream from this offset.

int $maxlen

Stream at most $maxlen bytes, or all if $maxlen is less than 1.

Return Value

Body self.

Body toStream(resource $stream, int $offset = 0, int $maxlen = 0)

Stream the message body into another stream $stream, starting from $offset, streaming $maxlen at most.

Parameters

resource $stream

The resource to write to.

int $offset

The starting offset.

int $maxlen

The maximum amount of data to stream. All content if less than 1.

Return Value

Body self.

string toString()

Retrieve the message body serialized to a string.

Alias of http\Message\Body::__toString().

Return Value

string

message body.

void unserialize(string $serialized)

Implements Serializable.

Parameters

string $serialized

The serialized message body.

Return Value

void