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

Schwab: List recent orders for one account

See more Schwab Examples

Lists orders entered in the last seven days, with an optional status filter such as WORKING. Shows the order ID, entry time, status, filled quantity, and remaining quantity. Select an ID from the results for the order detail and fills example. An older order can still be working outside this entry-time window.

Use the access token saved by desktop authorization. First select the authorized account whose data you want to use. Use the same Chilkat Secrets store as the OAuth example.

Schwab API specification

Chilkat PHP ActiveX Downloads

PHP ActiveX
<?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;

//  Read the hash saved by the authorized-account selection example.
$entry->UpdateString('service','SelectedAccount');
$selection = new COM("Chilkat.JsonObject");
$success = $vault->GetSecretJson($entry,$selection);
$accountHash = $selection->stringOf('hashValue');
if (($success != 1) or ($accountHash == '')) {
    print 'Select an authorized account first.' . "\n";
    exit;
}

$http->SetUrlVar('account',$accountHash);

//  Use UTC endpoints for a rolling seven-day window.
$clock = new COM("Chilkat.CkDateTime");
$clock->SetFromCurrentSystemTime();
$endTime = new COM("Chilkat.StringBuilder");
$endTime->Append($clock->getAsTimestamp(0));
$replacements = $endTime->Replace('Z','.000Z');
$clock->AddDays(-7);
$startTime = new COM("Chilkat.StringBuilder");
$startTime->Append($clock->getAsTimestamp(0));
$replacements = $startTime->Replace('Z','.000Z');
$statusFilter = '';
$resp = new COM("Chilkat.HttpResponse");
$query = new COM("Chilkat.HttpRequest");
$query->HttpVerb = 'GET';
$query->AddParam('fromEnteredTime',$startTime->getAsString());
$query->AddParam('toEnteredTime',$endTime->getAsString());
$query->AddParam('maxResults','100');
if ($statusFilter != '') {
    $query->AddParam('status',$statusFilter);
}

$success = $http->HttpReq('https://api.schwabapi.com/trader/v1/accounts/{$account}/orders',$query,$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;
}

$orders = new COM("Chilkat.JsonArray");
$success = $orders->Load($resp->BodyStr);
if ($success != 1) {
    print 'Schwab returned unexpected JSON.' . "\n";
    exit;
}

if ($orders->Size == 0) {
    print 'No matching orders entered during this interval.' . "\n";
}

if ($orders->Size == 100) {
    print 'Reached maxResults. Narrow the interval to check for additional orders.' . "\n";
}

$order = new COM("Chilkat.JsonObject");
$i = 0;
while (($i < $orders->Size)) {
    $orders->ObjectAt2($i,$order);
    print 'Order ' . $order->stringOf('orderId') . '  ' . $order->stringOf('enteredTime') . '  ' . $order->stringOf('orderType') . '  ' . $order->stringOf('status') . "\n";
    print '  filled ' . $order->stringOf('filledQuantity') . '  remaining ' . $order->stringOf('remainingQuantity') . "\n";
    $i = $i + 1;
}


?>