Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

Schwab: List authorized accounts and select one

See more Schwab Examples

Lists masked account numbers. Run once to see their endings, then enter the last four digits of the intended account. A unique match saves Schwab’s hashValue in a separate secret for the account-specific examples. If two accounts share an ending, use the full-account input from your account picker. A failed match leaves the previous selection in place.

Use the access token saved by desktop authorization. Use the same Chilkat Secrets store as the OAuth example.

Schwab API specification

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonArray;
use Chilkat2\JsonObject;
use Chilkat2\Secrets;
use Chilkat2\StringBuilder;

$success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = false;

$vault = new Secrets();
$vault->Location = 'local_manager';
$vault->AutoSplit = true;
$entry = new JsonObject();
$entry->UpdateString('appName', 'SchwabTrader');
$entry->UpdateString('service', 'OAuth2');
$entry->UpdateString('username', 'personal-desktop');
$tokens = new JsonObject();
try {
    $vault->GetSecretJson($entry, $tokens);
} catch (\Chilkat2\Exception $e) {
    echo "Could not read the token record. Check the store and complete desktop authorization first.\n";
    exit;
}

$accessToken = $tokens->StringOf('access_token');
if ($accessToken === '') {
    echo "Run the Schwab authorization or refresh example to save an access token.\n";
    exit;
}

$http = new Http();
$http->RequireSslCertVerify = true;
$http->AuthToken = $accessToken;

// Supply one of these from your account picker. Leave both empty to list only.
$selectedEnding = '';
$selectedFullNumber = '';
$resp = new HttpResponse();
try {
    $http->HttpNoBody('GET', 'https://api.schwabapi.com/trader/v1/accounts/accountNumbers', $resp);
    $success = true;
} catch (\Chilkat2\Exception $e) {
    $success = false;
}
if ((!$success) || ($resp->StatusCode != 200)) {
    if ($resp->StatusCode == 0) {
        echo "No HTTP response. Check connectivity.\n";
    } else {
        echo 'Schwab returned HTTP ', $resp->StatusCode, "\n";
        if ($resp->StatusCode == 401) {
            echo "Refresh the access token or repeat desktop authorization.\n";
        }
    }

    exit;
}

$accounts = new JsonArray();
try {
    $accounts->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
    echo "Schwab returned unexpected JSON.\n";
    exit;
}

$row = new JsonObject();
$number = new StringBuilder();
$matches = 0;
$i = 0;
$selectedHash = '';
while ($i < $accounts->Size) {
    $accounts->ObjectAt2($i, $row);
    $accountNumber = $row->StringOf('accountNumber');
    $number->SetString($accountNumber);
    if ($number->Length >= 4) {
        $start = $number->Length - 4;
        $ending = $number->GetRange($start, 4, false);
        echo 'Account ending in ', $ending, "\n";
        $matched = false;
        if ($selectedFullNumber !== '') {
            if ($accountNumber === $selectedFullNumber) {
                $matched = true;
            }
        } else {
            if ($ending === $selectedEnding) {
                $matched = true;
            }
        }

        if ($matched) {
            $matches = $matches + 1;
            $selectedHash = $row->StringOf('hashValue');
        }
    }

    $i = $i + 1;
}

if (($matches != 1) || ($selectedHash === '')) {
    echo "Select exactly one account. For duplicate endings use the full-account input.\n";
    exit;
}

$entry->UpdateString('service', 'SelectedAccount');
$selection = new JsonObject();
$selection->UpdateString('hashValue', $selectedHash);
try {
    $vault->UpdateSecretJson($entry, $selection);
} catch (\Chilkat2\Exception $e) {
    echo "Could not save the selected account.\n";
    exit;
}

echo "Selected account saved.\n";