Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion php_example/composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"require": {
"firebase/php-jwt": "^5.2"
"firebase/php-jwt": "^6.11",
"ext-curl": "*",
"ext-readline": "*"
}
}
50 changes: 19 additions & 31 deletions php_example/enablebanking.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

include 'vendor/autoload.php';

use \Firebase\JWT\JWT;
use Firebase\JWT\JWT;

function request($url, $headers=[], $post='')
function request($url, $headers = [], $post = ''): array
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
if(!empty($post))
{
if (!empty($post)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if(count($headers))
{
if (count($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}

Expand All @@ -32,7 +30,7 @@ function request($url, $headers=[], $post='')
$rsa_key = file_get_contents(dirname(__FILE__) . '/../' . $key_path);

// Creating JWT
$jwt_header = [ 'typ' => 'JWT', 'alg' => 'RS256', 'kid' => $config->applicationId ];
$jwt_header = ['typ' => 'JWT', 'alg' => 'RS256', 'kid' => $config->applicationId];
$payload = [
'iss' => 'enablebanking.com',
'aud' => 'api.enablebanking.com',
Expand All @@ -48,8 +46,7 @@ function request($url, $headers=[], $post='')

// Requesting application details
$r = request('https://api.enablebanking.com/application', $headers);
if($r['status'] === 200)
{
if ($r['status'] === 200) {
$app = json_decode($r['body']);
echo 'Application details:';
print_r($app);
Expand All @@ -59,8 +56,7 @@ function request($url, $headers=[], $post='')

// Requesting available ASPSPs
$r = request('https://api.enablebanking.com/aspsps', $headers);
if($r['status'] === 200)
{
if ($r['status'] === 200) {
$aspsps = json_decode($r['body']);
echo 'Available ASPSPs:';
print_r($aspsps);
Expand All @@ -71,16 +67,15 @@ function request($url, $headers=[], $post='')
// Starting authorization
$valid_until = time() + 2 * 7 * 24 * 60 * 60;
$body = [
'access' => [ 'valid_until' => date('c', $valid_until) ],
'aspsp' => [ 'name' => 'Danske Bank', 'country' => 'FI' ], // { name: aspsps[0]['name'], country: aspsps[0]['country'] },
'access' => ['valid_until' => date('c', $valid_until)],
'aspsp' => ['name' => 'Danske Bank', 'country' => 'FI'], // { name: aspsps[0]['name'], country: aspsps[0]['country'] },
'state' => 'random',
'redirect_url' => $app->redirect_urls[0],
'psu_type' => 'personal'
];

$r = request('https://api.enablebanking.com/auth', $headers, json_encode($body));
if($r['status'] == 200)
{
if ($r['status'] == 200) {
$auth_url = json_decode($r['body'])->url;
echo 'To authenticate open URL ' . $auth_url . PHP_EOL;
} else {
Expand All @@ -90,22 +85,20 @@ function request($url, $headers=[], $post='')
// Reading auth code and creating user session
$auth_code = readline('Enter value of code parameter from the URL you were redirected to: ');

$body = json_encode([ 'code' => $auth_code ]);
$body = json_encode(['code' => $auth_code]);
$r = request('https://api.enablebanking.com/sessions', $headers, $body);
if($r['status'] === 200)
{
if ($r['status'] === 200) {
$session = json_decode($r['body']);
echo 'New user session has been created:';
print_r($session);
print_r($session);
} else {
exit('Error response #' . $r['status'] . ':' . $r['body']);
}
$account_uid = $session->accounts[0]->uid;

// Retrieving account balances
$r = request('https://api.enablebanking.com/accounts/' . $account_uid . '/balances', $headers);
if($r['status'] === 200)
{
if ($r['status'] === 200) {
$balances = json_decode($r['body'])->balances;
echo 'Balances: ';
print_r($balances);
Expand All @@ -115,22 +108,18 @@ function request($url, $headers=[], $post='')

// Retrieving account transactions (since yesterday)
$continuation_key = null;
do
{
do {
$params = '?date_from=' . date('Y-m-d', strtotime('-1 day', time()));
if($continuation_key)
{
if ($continuation_key) {
$params .= '&continuation_key=' . $continuation_key;
}
$r = request('https://api.enablebanking.com/accounts/' . $account_uid . '/transactions' . $params, $headers);
if($r['status'] === 200)
{
if ($r['status'] === 200) {
$rest_data = json_decode($r['body']);
$transactions = $rest_data->transactions;
echo 'Transactions:';
print_r($transactions);
if(isset($rest_data->continuation_key) && $rest_data->continuation_key)
{
if (isset($rest_data->continuation_key) && $rest_data->continuation_key) {
$continuation_key = $rest_data->continuation_key;
echo 'Going to fetch more transaction with continaution key ' . $continuation_key;
} else {
Expand All @@ -140,5 +129,4 @@ function request($url, $headers=[], $post='')
} else {
exit('Error response #' . $r['status'] . ':' . $r['body']);
}
}
while(true);
} while (true);