Skip to content

PSR 7: Message Example

Terry L edited this page Jun 20, 2020 · 5 revisions
Clone this wiki locally

PSR-7 HTTP Message Interfaces

Namespace

Shieldon\Psr7\Message

Message

__construct

None

Example:

$message = new \Shieldon\Psr7\Message();

getProtocolVersion()

  • return string HTTP protocol version.

Example:

echo $message->getProtocolVersion();
// Outputs: 1.1

withProtocolVersion($version)

  • param string version * HTTP protocol version.
  • return static

getHeaders()

  • return array (string[][]) Each key is a header name, and each value is an array of strings for that header

Example:

$headers = $message->getHeaders();

print(print_r($headers, true));

/* Outputs:

Array
(
    [user-agent] => Array
        (
            [0] => Mozilla/5.0 (Windows NT 10.0; Win64; x64)
        )

    [host] => Array
        (
            [0] => 127.0.0.1
        )

    [accept] => Array
        (
            [0] => text/html,application/xhtml+xml,application/xml;q=0.9
        )

    [accept_charset] => Array
        (
            [0] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
        )
    [accept_language] => Array
        (
            [0] => en-US,en;q=0.9,zh-TW;q=0.8,zh;q=0.7
        )
)

*/

hasHeader($name)

  • param string name * Case-insensitive header field name.
  • return bool

Example:

if ($message->hasHeader('user-agent')) {
    echo 'Header user-agent exists.';
} else {
    echo 'Header user-agent does not exist.';
}
// Outputs: Header user-agent exists.

getHeader($name)

  • param string name * Case-insensitive header field name.
  • return array An array of string values as provided for the given. Return empty array if the header dosn't exist.

Example:

$useragent = $this->message->getHeader('user-agent');

print(print_r($useragent, true));

/* Outputs:

    Array
    (
        [0] => Mozilla/5.0 (Windows NT 10.0; Win64; x64)
    )

*/

$useragent = $this->message->getHeader('does-not-exist');

print(print_r($useragent, true));

/* Outputs:

    Array()

*/

getHeaderLine($name)

  • param string name * Case-insensitive header field name.
  • return array a string values as provided for the given header concatenated together using a comma. Return empty string if the header dosn't exist.

Example:

echo $this->message->getHeaderLine('user-agent');

// Outputs: Mozilla/5.0 (Windows NT 10.0; Win64; x64)

withHeader($name, $value)

  • param string name * Case-insensitive header field name.
  • param string|array value * Header value(s)
  • return static

Example:

$message = $message->withHeader('foo', 'bar');

echo $message->getHeaderLine('foo');
// Outputs: bar

echo $message->getHeaderLine('FOO');
// Outputs: bar

$message = $message->withHeader('fOO', 'baz');
echo $message->getHeaderLine('foo');
// Outputs: baz

$message = $message->withHeader('fOO', ['bax', 'bay', 'baz']);
echo $message->getHeaderLine('foo');
// Outputs: bax, bay, baz

withAddedHeader($name, $value)

  • param string name * Case-insensitive header field name.
  • param string|array value * Header value(s)
  • return static

Existing values for the specified header will be maintained. The new value(s) will be appended to the existing list. If the header did not exist previously, it will be added.

Example:

$message = $message->withHeader('foo', 'bar');

echo $message->getHeaderLine('foo');
// Outputs: bar

$message = $message->withAddedHeader('foo', 'baz');
echo $message->getHeaderLine('foo');
// Outputs: bar

$message = $message->withAddedHeader('foo2', 'baz');
echo $message->getHeaderLine('foo2');
// Outputs: baz

withoutHeader($name)

  • param string name * Case-insensitive header field name.
  • return static

Example:

$message = $message->withHeader('foo', 'bar');

echo $message->getHeaderLine('foo');
// Outputs: bar

$message = $message->withoutHeader('foo');
echo $message->getHeaderLine('foo');
// Outputs: 

getBody()

  • return StreamInterface

Example:

$stream = $message->getBody();

// Assume the content is a HTML formatted string.
// getContent() is a method defined in StreamInterface.
echo $stream->getContents();

// Outputs: <html>...</html>

withBody($body)

  • param StreamInterface body * Body.
  • return static

Example:

$stream = new \Shieldon\Psr7\Stream(fopen('php://temp', 'r+'));
$stream->write('Foo Bar');

$message = $message->withBody($stream);

echo $message->getBody()->getContents();

// Outputs: Foo Bar