Core\Http\Response::status
$response->status(): intReturns the status code of a reply as an int, for example 200 or 404.
The status code is the number the server sends first to say how the request went. A code from 200 to 299 means that it worked. A code of 400 or more means an error. An error code does not throw an error in your program: the request still returns a reply, and your program reads status() and decides what to do. status() is a method, so you write $response->status(), with brackets.
Good to know: the examples use Core\Test::answerHttp to give fixed replies, so they run without a network. They read a status code, handle a page that was not found, and check a list of links for broken ones.
Return value
Returns int. The three-digit code from the status line. A 404 or a 500 is a normal reply and is returned here. Only a request that gets no reply at all throws an error.
Examples
Reading the status code
<?nvs// Shows how to read the status code of a reply.
// `Core\Test::answerHttp` gives a fixed reply to this address, so the example runs// without a network.Core\Test::answerHttp('https://api.example.com/health', 200, { body: 'ok' });
Core\Http\Response $response = Core\Http\Client::get('https://api.example.com/health');
// `status()` returns the code as an `int`. This prints 200.echo $response->status(), "\n";
// 200 means that the request worked. This prints `The service is up.`if ($response->status() == 200) { echo "The service is up.\n";}200The service is up.A page that was not found
<?nvs// Shows that an error status such as 404 is returned like any other status.
// The server replies with 404, which means "not found".Core\Test::answerHttp('https://api.example.com/users/9', 404, { body: 'no such user' });
// `Core\Http\Client::get` does not throw an error for a 404.// The program reads the status and decides what to do.Core\Http\Response $response = Core\Http\Client::get('https://api.example.com/users/9');
// This prints `404: no such user`.if ($response->status() == 404) { echo $response->status(), ': ', $response->text(), "\n";}404: no such userChecking links
<?nvs// Shows a program that checks a list of links and reports every link that is broken.
class LinkCheck{ // Returns a short report for one link. A status of 400 or more means an error. public static function report(string $url): string { int $status = Core\Http\Client::head($url)->status(); if ($status >= 400) { return 'broken (' . $status . '): ' . $url; } return 'ok (' . $status . '): ' . $url; }}
// Three fixed replies, so the example runs without a network.Core\Test::answerHttp('https://shop.example.com/', 200, {});Core\Test::answerHttp('https://shop.example.com/old-offer', 410, {});Core\Test::answerHttp('https://shop.example.com/cart', 503, {});
// This prints one line per link: the first is ok, the other two are broken.array<string> $links = [ 'https://shop.example.com/', 'https://shop.example.com/old-offer', 'https://shop.example.com/cart',];foreach ($links as string $link) { echo LinkCheck::report($link), "\n";}ok (200): https://shop.example.com/broken (410): https://shop.example.com/old-offerbroken (503): https://shop.example.com/cart