Documentation

Body
in package
implements StreamInterface

HTTP body class

Tags
category

Pop

author

Nick Sagona, III nick@popphp.org

copyright

Copyright (c) 2009-2026 Nick Sagona, III

license

https://www.popphp.org/license New BSD License

version
6.0.0

Table of Contents

Interfaces

StreamInterface

Constants

BASE64  = 'BASE64'
Encoding constants
QUOTED  = 'QUOTED'
RAW_URL  = 'RAW_URL'
URL  = 'URL'

Properties

$encoding  : string|null
Encoding
$isEncoded  : bool
Is encoded flag
$isFile  : bool
Is file flag
$split  : int|bool|null
Chunk split
$stream  : resource|null
Underlying content stream. All content - string-set or file-backed - lives here.

Methods

__clone()  : void
Deep-duplicate the underlying stream resource so a clone never shares a read/write position with the instance it was cloned from
__construct()  : mixed
Constructor
__toString()  : string
Render the body
close()  : void
Close the underlying stream
detach()  : mixed
Detach the underlying stream resource, leaving the Body empty
eof()  : bool
Determine if the stream is at end-of-file
getContent()  : string
Get the body content as a string
getContents()  : string
Get the remaining raw stream contents, per PSR-7 - bypasses render()'s encoding/split transforms, unlike __toString()
getEncoding()  : string|null
Get the encoding
getMetadata()  : mixed
Get stream metadata
getSize()  : int|null
Get the content size in bytes, without reading file-backed content into memory to compute it. Per PSR-7, returns null if the stream size is not statable (e.g. php://output).
getSplit()  : int|bool|null
Get the split
getStream()  : mixed
Get the raw underlying stream resource, for a consumer that wants to copy bytes directly (e.g. to the network) without materializing the whole content as a PHP string.
hasContent()  : bool
Has body content
hasEncoding()  : bool
Has encoding
hasSplit()  : bool
Has split
isBase64Encoding()  : bool
Is encoding base64
isEncoded()  : bool
Is encoded
isFile()  : bool
Is file
isQuotedEncoding()  : bool
Is encoding quoted-printable
isRawUrlEncoding()  : bool
Is encoding raw URL
isReadable()  : bool
Determine if the stream is readable
isSeekable()  : bool
Determine if the stream is seekable
isUrlEncoding()  : bool
Is encoding URL
isWritable()  : bool
Determine if the stream is writable
read()  : string
Read up to $length bytes from the stream
render()  : string
Render the body
rewind()  : void
Seek to the beginning of the stream
seek()  : void
Seek to a position in the stream
setAsEncoded()  : Body
Set as encoded
setAsFile()  : Body
Set as file
setContent()  : Body
Set the body content from a string
setContentFromFile()  : Body
Set the body content from a file, without buffering it into memory
setContentFromStream()  : Body
Set the body content directly from an existing stream resource
setEncoding()  : Body
Set the encoding
setSplit()  : Body
Set the split
tell()  : int
Get the current position of the stream read/write pointer
write()  : int
Write data to the stream

Constants

BASE64

Encoding constants

public string BASE64 = 'BASE64'

QUOTED

public mixed QUOTED = 'QUOTED'

RAW_URL

public mixed RAW_URL = 'RAW_URL'

URL

public mixed URL = 'URL'

Properties

$encoding

Encoding

protected string|null $encoding = null

$isEncoded

Is encoded flag

protected bool $isEncoded = false

$isFile

Is file flag

protected bool $isFile = false

$split

Chunk split

protected int|bool|null $split = null

$stream

Underlying content stream. All content - string-set or file-backed - lives here.

protected resource|null $stream = null

Methods

__clone()

Deep-duplicate the underlying stream resource so a clone never shares a read/write position with the instance it was cloned from

public __clone() : void

__construct()

Constructor

public __construct([string|null $content = null ][, string|null $encoding = null ][, int|bool|null $split = null ]) : mixed

Instantiate the body object

Parameters
$content : string|null = null
$encoding : string|null = null
$split : int|bool|null = null

__toString()

Render the body

public __toString() : string
Return values
string

close()

Close the underlying stream

public close() : void

detach()

Detach the underlying stream resource, leaving the Body empty

public detach() : mixed

eof()

Determine if the stream is at end-of-file

public eof() : bool
Return values
bool

getContent()

Get the body content as a string

public getContent() : string

Reads the entire underlying stream and restores its read position afterward, so repeated calls return the same content.

Return values
string

getContents()

Get the remaining raw stream contents, per PSR-7 - bypasses render()'s encoding/split transforms, unlike __toString()

public getContents() : string
Tags
throws
Exception
Return values
string

getEncoding()

Get the encoding

public getEncoding() : string|null
Return values
string|null

getMetadata()

Get stream metadata

public getMetadata([string|null $key = null ]) : mixed
Parameters
$key : string|null = null

getSize()

Get the content size in bytes, without reading file-backed content into memory to compute it. Per PSR-7, returns null if the stream size is not statable (e.g. php://output).

public getSize() : int|null
Return values
int|null

getSplit()

Get the split

public getSplit() : int|bool|null
Return values
int|bool|null

getStream()

Get the raw underlying stream resource, for a consumer that wants to copy bytes directly (e.g. to the network) without materializing the whole content as a PHP string.

public getStream() : mixed

hasContent()

Has body content

public hasContent() : bool
Return values
bool

hasEncoding()

Has encoding

public hasEncoding() : bool
Return values
bool

hasSplit()

Has split

public hasSplit() : bool
Return values
bool

isBase64Encoding()

Is encoding base64

public isBase64Encoding() : bool
Return values
bool

isEncoded()

Is encoded

public isEncoded() : bool
Return values
bool

isFile()

Is file

public isFile() : bool
Return values
bool

isQuotedEncoding()

Is encoding quoted-printable

public isQuotedEncoding() : bool
Return values
bool

isRawUrlEncoding()

Is encoding raw URL

public isRawUrlEncoding() : bool
Return values
bool

isReadable()

Determine if the stream is readable

public isReadable() : bool
Return values
bool

isSeekable()

Determine if the stream is seekable

public isSeekable() : bool
Return values
bool

isUrlEncoding()

Is encoding URL

public isUrlEncoding() : bool
Return values
bool

isWritable()

Determine if the stream is writable

public isWritable() : bool
Return values
bool

read()

Read up to $length bytes from the stream

public read(int $length) : string
Parameters
$length : int
Tags
throws
Exception
Return values
string

render()

Render the body

public render() : string
Return values
string

rewind()

Seek to the beginning of the stream

public rewind() : void
Tags
throws
Exception

seek()

Seek to a position in the stream

public seek(int $offset[, int $whence = SEEK_SET ]) : void
Parameters
$offset : int
$whence : int = SEEK_SET
Tags
throws
Exception

setAsEncoded()

Set as encoded

public setAsEncoded(bool $isEncoded) : Body
Parameters
$isEncoded : bool
Return values
Body

setAsFile()

Set as file

public setAsFile(bool $isFile) : Body
Parameters
$isFile : bool
Return values
Body

setContent()

Set the body content from a string

public setContent(string $content) : Body
Parameters
$content : string
Return values
Body

setContentFromFile()

Set the body content from a file, without buffering it into memory

public setContentFromFile(string $file[, string|null $encoding = null ][, int|bool|null $split = null ]) : Body
Parameters
$file : string
$encoding : string|null = null
$split : int|bool|null = null
Tags
throws
Exception
Return values
Body

setContentFromStream()

Set the body content directly from an existing stream resource

public setContentFromStream(mixed $stream) : Body

If the given stream is known to be backed by a real file (e.g. a handle returned from fopen() on a local file), chain ->setAsFile(true) afterward to flag it as such, since this method has no reliable way to detect that on its own.

Parameters
$stream : mixed
Return values
Body

setEncoding()

Set the encoding

public setEncoding(string $encoding) : Body
Parameters
$encoding : string
Return values
Body

setSplit()

Set the split

public setSplit(int|bool $split) : Body
Parameters
$split : int|bool
Return values
Body

tell()

Get the current position of the stream read/write pointer

public tell() : int
Tags
throws
Exception
Return values
int

write()

Write data to the stream

public write(string $string) : int
Parameters
$string : string
Tags
throws
Exception
Return values
int

        
On this page

Search results