PHP ActiveX Requires Chilkat v11.0.0+
PHP ActiveX
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.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$vault = new COM("Chilkat.Secrets");
$vault->Location = 'local_manager';
$vault->AutoSplit = 1;
$entry = new COM("Chilkat.JsonObject");
$entry->UpdateString('appName','SchwabTrader');
$entry->UpdateString('service','OAuth2');
$entry->UpdateString('username','personal-desktop');
$tokens = new COM("Chilkat.JsonObject");
$success = $vault->GetSecretJson($entry,$tokens);
if ($success != 1) {
print 'Could not read the token record. Check the store and complete desktop authorization first.' . "\n";
exit;
}
$accessToken = $tokens->stringOf('access_token');
if ($accessToken == '') {
print 'Run the Schwab authorization or refresh example to save an access token.' . "\n";
exit;
}
$http = new COM("Chilkat.Http");
$http->RequireSslCertVerify = 1;
$http->AuthToken = $accessToken;
// Supply one of these from your account picker. Leave both empty to list only.
$selectedEnding = '';
$selectedFullNumber = '';
$resp = new COM("Chilkat.HttpResponse");
$success = $http->HttpNoBody('GET','https://api.schwabapi.com/trader/v1/accounts/accountNumbers',$resp);
if (($success != 1) or ($resp->StatusCode != 200)) {
if ($resp->StatusCode == 0) {
print 'No HTTP response. Check connectivity.' . "\n";
}
else {
print 'Schwab returned HTTP ' . $resp->StatusCode . "\n";
if ($resp->StatusCode == 401) {
print 'Refresh the access token or repeat desktop authorization.' . "\n";
}
}
exit;
}
$accounts = new COM("Chilkat.JsonArray");
$success = $accounts->Load($resp->BodyStr);
if ($success != 1) {
print 'Schwab returned unexpected JSON.' . "\n";
exit;
}
$row = new COM("Chilkat.JsonObject");
$number = new COM("Chilkat.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,0);
print 'Account ending in ' . $ending . "\n";
$matched = 0;
if ($selectedFullNumber != '') {
if ($accountNumber == $selectedFullNumber) {
$matched = 1;
}
}
else {
if ($ending == $selectedEnding) {
$matched = 1;
}
}
if ($matched == 1) {
$matches = $matches + 1;
$selectedHash = $row->stringOf('hashValue');
}
}
$i = $i + 1;
}
if (($matches != 1) or ($selectedHash == '')) {
print 'Select exactly one account. For duplicate endings use the full-account input.' . "\n";
exit;
}
$entry->UpdateString('service','SelectedAccount');
$selection = new COM("Chilkat.JsonObject");
$selection->UpdateString('hashValue',$selectedHash);
$success = $vault->UpdateSecretJson($entry,$selection);
if ($success != 1) {
print 'Could not save the selected account.' . "\n";
exit;
}
print 'Selected account saved.' . "\n";
?>