PHP
Chilkat2 PHP
Enumerate REST Response Header Names
See more REST Examples
Demonstrates Rest.ResponseHdrName, which returns the field name of the response header at a zero-based index. The example enumerates all headers, pairing each name with its value.
Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one. ResponseHdrName and ResponseHdrValue use the same index for a given header.
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;
}
// Enumerate the response header fields by zero-based index. ResponseHdrName returns the field name
// and ResponseHdrValue returns the value at the same index.
$numHeaders = $rest->NumResponseHeaders;
for ($i = 0; $i < $numHeaders; $i++) {
try {
$name = $rest->ResponseHdrName($i);
$value = $rest->ResponseHdrValue($i);
} catch (\Chilkat2\Exception $e) {
echo $e->getMessage(), "\n";
exit;
}
echo $name, ': ', $value, "\n";
}