GUS API – cakePHP 5

https://github.com/johnzuk/GusApi

https://api.stat.gov.pl/Home/RegonApi

# composer require gusapi/gusapi

Using version ^6.3 for gusapi/gusapi

Tworzymy kontroler GusController,  prefix Api

# bin/cake bake controller Gus --prefix Api
<?php 
declare(strict_types=1); 

namespace App\Controller\Api; 

use App\Controller\AppController; 
use App\Utility\NipHelper; 
use GusApi\Exception\InvalidUserKeyException; 
use GusApi\Exception\NotFoundException; 
use GusApi\GusApi; 
use GusApi\ReportTypes; 
use GusApi\BulkReportTypes; 

/** 
* Gus Controller 
* 
*/ 
class GusController extends AppController 
{ 
  private $gusApi = '__myapikey'; 

  public function beforeFilter(\Cake\Event\EventInterface $event): void 
  { 
    parent::beforeFilter($event); 
    // $this->Authentication->addUnauthenticatedActions(['getGusReport']);
  } 

    // ================== AJAX ===================

    public function getGusReport()
    {
        if($this->request->is('ajax')) {
            $nip = $this->request->getParsedBody()['nip'];
            // $year = $this->request->getParsedBody()['year'];

            try {
                $gus = new GusApi($this->gusApi);

                $gus->login();
                $report = $gus->getByNip($nip);

                $this->set('report', $report);
                $this->viewBuilder()->setOption('serialize', ['report']);
            } catch (InvalidUserKeyException $e) {
                $this->Flash->error('Nieprawidłowy klucz użytkownika Gus.');
            } catch (NotFoundException $e) {
                $this->Flash->error('NIP nie został znaleziony w bazie Gus.');
            }
        } // ajax
    } // getGusReport()
} // GusController