Sample code for 30+ languages & platforms
PHP

Get a REST Response Header Value by Index

See more REST Examples

Demonstrates Rest.ResponseHdrValue, which returns the value of the response header at a zero-based index. The same index used with ResponseHdrName gives the corresponding field name.

Background. Response headers can be enumerated by index from 0 through NumResponseHeaders minus one, after ReadResponseHeader has been called.

Chilkat PHP Downloads

Chilkat2 PHP
<?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 the response header at a zero-based index.  Use the same index with
// ResponseHdrName to get the corresponding field name.
$numHeaders = $rest->NumResponseHeaders;
for ($i = 0; $i < $numHeaders; $i++) {
    try {
        $value = $rest->ResponseHdrValue($i);
    } catch (\Chilkat2\Exception $e) {
        echo $e->getMessage(), "\n";
        exit;
    }

    echo 'Header ', $i, ' value: ', $value, "\n";
}