diff --git a/php_example/composer.json b/php_example/composer.json index c4818db..4210dc5 100644 --- a/php_example/composer.json +++ b/php_example/composer.json @@ -1,5 +1,7 @@ { "require": { - "firebase/php-jwt": "^5.2" + "firebase/php-jwt": "^6.11", + "ext-curl": "*", + "ext-readline": "*" } } diff --git a/php_example/enablebanking.php b/php_example/enablebanking.php index 1439856..799dc49 100644 --- a/php_example/enablebanking.php +++ b/php_example/enablebanking.php @@ -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); } @@ -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', @@ -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); @@ -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); @@ -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 { @@ -90,13 +85,12 @@ 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']); } @@ -104,8 +98,7 @@ function request($url, $headers=[], $post='') // 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); @@ -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 { @@ -140,5 +129,4 @@ function request($url, $headers=[], $post='') } else { exit('Error response #' . $r['status'] . ':' . $r['body']); } -} -while(true); +} while (true);