PHP
Chilkat2 PHP
Get a REST Response Header by Name
See more REST Examples
Demonstrates Rest.ResponseHdrByName, which returns the value of a response header field by name. Header field names are case-insensitive.
Background. Response headers are available after ReadResponseHeader has read the status line and headers. Lookup by name is convenient when a specific header, such as Content-Type, is needed.
Chilkat PHP Downloads
<?php
use Chilkat2\Rest;
$rest = new Rest();
$bTls = true;
$bAutoReconnect = true;
try {
$rest->Connect('example.com', 443, $bTls, $bAutoReconnect);
$rest->SendReqNoBody('GET', '/api/items/123');
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
$statusCode = $rest->ReadResponseHeader();
if ($statusCode < 0) {
echo $rest->LastErrorText, "\n";
exit;
}
// Return the value of a response header field by name. Header field names are case-insensitive.
$contentType = $rest->ResponseHdrByName('Content-Type');
if ($contentType === null) {
echo $rest->LastErrorText, "\n";
exit;
}
echo 'Content-Type: ', $contentType, "\n";