Core\Http\Response::text
Returns the body of a reply as a string.
Use text() for a reply that is text, for example a web page, a CSV file or a plain version number. The body must be valid UTF-8 text. If it is not, text() throws a RuntimeError. The message says which byte is the first one that is not text. bytes() returns the same body without this check. You can call text() more than once, and each call returns the same text.
The body comes from another server, so the result is tainted until your program checks it.
Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. They read a small text file, show a reply in an old encoding that is not UTF-8, and check an update server for a new version number.
Return value
Returns tainted string. The body, as a tainted string. Another server chose these bytes. The approved address tells you where they came from, but not what they contain. Each place that does not accept tainted text has its own check to clean it. There is no general one.
Errors
RuntimeErrorThe body is not valid UTF-8, so it cannot be astring. The body is not repaired. A replacement character would change the body, and the program could not tell.bytesreads the same body without this check.
Examples
Reading a text file
<?nvs// Shows how to read the body of a reply as text.
// `Core\Test::answerHttp` gives a fixed reply to this address, so the example runs// without a network. The body is a small text file with two lines.Core\Test::answerHttp('https://example.com/robots.txt', 200, { body: "User-agent: *\nDisallow: /admin/\n", headers: ['Content-Type' => 'text/plain'],});
Core\Http\Response $response = Core\Http\Client::get('https://example.com/robots.txt');
// `text()` returns the body as a `string`. This prints both lines of the file.tainted string $rules = $response->text();echo $rules;
// `Core\Str::lines` splits the text into lines. This prints 2.echo Core\Arr::count(Core\Str::lines($rules)), "\n";User-agent: *Disallow: /admin/2A reply in an old encoding
<?nvs// Shows what happens when a server sends text in an old encoding, not in UTF-8.
// The body is the word "Größe" in the old Latin-1 encoding. In Latin-1, "ö" is// the single byte f6. That byte never occurs in UTF-8 text.Core\Test::answerHttp('https://old.example.com/size.txt', 200, { body: Core\Encoding::fromHex('4772f6df65'),});Core\Http\Response $response = Core\Http\Client::get('https://old.example.com/size.txt');
// `text()` checks that the body is UTF-8 text. This body is not, so the call throws// a `RuntimeError`. The message names byte 2, counted from 0, as the first bad byte.try { echo $response->text(), "\n";} catch (RuntimeError $error) { echo $error->message, "\n";}
// `bytes()` still returns the whole body. This prints 5.echo Core\Bytes::length($response->bytes()), "\n";Core\Http\Response::text: the reply's body is not valid UTF-8, so it is not a `string` — byte 2 is where it stops being text, and `bytes()` reads it whole5Checking for a new version
<?nvs// Shows a program that checks whether a newer version of itself exists.// The update server returns the latest version number as plain text.
class Updates{ // Returns the latest version number, without spaces or line breaks around it. public static function latest(string $url): tainted string { Core\Http\Response $response = Core\Http\Client::get($url); return Core\Str::trim($response->text()); }}
// A fixed reply, so the example runs without a network.Core\Test::answerHttp('https://updates.example.com/latest.txt', 200, { body: "2.4.1\n" });
string $installed = '2.3.0';tainted string $latest = Updates::latest('https://updates.example.com/latest.txt');
// This prints `A new version is available: 2.4.1`.if ($latest != $installed) { echo 'A new version is available: ', $latest, "\n";} else { echo "You have the latest version.\n";}A new version is available: 2.4.1