Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Open Bank Project Example on parsing transaction data
<?php
define('DS', DIRECTORY_SEPARATOR);
$offset = 0;
$limit = 100;
$path = 'banking' . DS;
$url = 'https://api.openbankproject.com/obp/v1.2.1/banks/postbank/accounts/tesobe/public/transactions';
$gross_amount=0;
@mkdir($path);
while (true) {
$opts = array('http' => array('method' => "GET",
'header' => "obp_limit: $limit\r\n" . "obp_offset: $offset\r\n"));
$cache_fp = $path . md5($url) . "-$limit-$offset";
//echo $cache_fp . "\n";
if (!file_exists($cache_fp)) {
echo "Retrieving data from the web\n";
$file = file_get_contents($url, false, stream_context_create($opts));
file_put_contents($cache_fp, $file);
} else {
//echo "Retrieving data from cache\n";
$file = file_get_contents($cache_fp);
}
$data = json_decode($file);
foreach ($data->transactions as $item) {
print($item->details->completed . "\t");
print($item->other_account->holder->name . "\t");
print($item->details->value->amount . "\t");
$gross_amount +=$item->details->value->amount;
print($item->details->value->currency . "\t");
print("\n");
}
if (count($data->transactions) < $limit) {
break;
} else {
$offset+=$limit;
}
}
print("$gross_amount \n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment