Sample code for 30+ languages & platforms
PHP

Configure HTTP Basic Authentication with SecureString

See more REST Examples

Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.

Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\Rest;
use Chilkat2\SecureString;

$rest = new Rest();
$bTls = true;
$bAutoReconnect = true;
try {
    $rest->Connect('example.com', 443, $bTls, $bAutoReconnect);

    // SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
    // the credentials encrypted in memory.
    // Normally you would not hard-code secrets in source.  You should instead obtain them from an
    // interactive prompt, environment variable, or a secrets vault.
    $ssUsername = new SecureString();
    $ssUsername->Append('myUsername');
    $ssPassword = new SecureString();
    $ssPassword->Append('myPassword');

    $rest->SetAuthBasicSecure($ssUsername, $ssPassword);

    $responseText = $rest->FullRequestNoBody('GET', '/api/protected');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo $responseText, "\n";