PHP SDK: Response: Website Reply
The $response variable below comes from $ctx->http()->get($url). A response has methods and read-only properties.
Methods
Response properties
| Property | Meaning |
|---|---|
status |
HTTP status code, such as 200 or 404 |
body |
Response body as a string |
finalUrl |
URL after redirects |
headers |
Response headers |
fromCache |
Whether the response came from the cache |
durationMs |
Duration in milliseconds |
snapshotUuid |
Snapshot UUID or null |
ok
ok(): bool
Check that the HTTP status is between 200 and 299 and the response is not a detected CAPTCHA challenge. An HTTP 200 challenge page does not mean the requested data is available.
if (!$response->ok()) { $ctx->abort('Source returned HTTP ' . $response->status); }
Result and notes. Returns a Boolean. This checks status, not completeness of the data.
dom
dom(): Dom
Parse the response body as HTML, using the final URL after redirects.
$dom = $response->dom();
$title = $dom->text('h1');
Result and notes. Returns a Dom. Relative links are resolved against finalUrl.
json
json(): array
Parse the response body as JSON.
$data = $response->json();
$items = $data['items'] ?? [];
Result and notes. Returns an array. Invalid JSON throws JsonException. The root must be an object or array.
header
header(string $name): ?string
Get a response header without regard to the letter case of its name.
$mime = $response->header('Content-Type');
Result and notes. Returns a string or null. If there are several values, the first is returned.