GUS API

Konfiguracja i przykład użycia w CakePHP 4 – pakiet gusapi/gusapi v.6.1.1

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

  1. Wysłać e-mail na adres: Regon_Bir@stat.gov.pl
  2. Uzyskać API_KEY
  3. Instalacja pakietu
# composer require gusapi/gusapi

4. Aplikacja / controller

use GusApi\Exception\InvalidUserKeyException;
use GusApi\Exception\NotFoundException;
use GusApi\GusApi;
use GusApi\ReportTypes;
use GusApi\BulkReportTypes;

...
// ==== AJAX ====
public function getFromGus()
{
  if( $this->request->is('ajax'))
  {
    
    $input = $this->request->getParsedBody();
    $nip   = htmlspecialchars($input['nip']);
    
    if($nip) {
     try {
   
       $gus = new GusApi('api_key');
       $gus->login();
       $gusReports = $gus->getByNip($nip);

       $gusResults = [];
  
       foreach ($gusReports as $ind => $gusReport) 
       {
         $nr_domu   = $gusReport->getPropertyNumber()  ? $gusReport->getPropertyNumber()  : '';
         $nr_lokalu = $gusReport->getApartmentNumber() ? $gusReport->getApartmentNumber() : '';
   
         $separator = $nr_domu && $nr_lokalu ? '/' : '';
   
         $gusResults[$ind]['company_name'] = $gusReport->getName();
         $gusResults[$ind]['street']       = $gusReport->getStreet();
         $gusResults[$ind]['number']       = $nr_domu . $separator . $nr_lokalu;
         $gusResults[$ind]['address']      = $gusReport->getStreet(). " ".$nr_domu . $separator . $nr_lokalu;
         $gusResults[$ind]['zip']          = $gusReport->getZipCode();
         $gusResults[$ind]['location']     = $gusReport->getCity();
       }
  
       $result = $gusResults;
    } catch (InvalidUserKeyException $e) {
       $result = ['error' => 'Błędny klucz użytkownika'];
    } catch (NotFoundException $e) {
       $result = ['error' => $gus->getResultSearchMessage()];
    }
  } // nip
 $this->set('result', $result);
 $this->viewBuilder()->setOption('serialize', ['result']);
 } // ajax
} // getFromGus

Przykład z dokumentacji

try {
    $nipToCheck = 'xxxxxxxxxx'; //change to valid nip value
    $gus->login();

    $gusReports = $gus->getByNip($nipToCheck);

    var_dump($gus->dataStatus());
    var_dump($gus->getBulkReport(
        new DateTimeImmutable('2019-05-31'),
        BulkReportTypes::REPORT_DELETED_LOCAL_UNITS
    ));

    foreach ($gusReports as $gusReport) {
        //you can change report type to other one
        $reportType = ReportTypes::REPORT_PERSON;
        echo $gusReport->getName();
        echo 'Address: ' 
         . $gusReport->getStreet() . ' ' 
         . $gusReport->getPropertyNumber() . '/' 
         . $gusReport->getApartmentNumber();

        $fullReport = $gus->getFullReport($gusReport, $reportType);
        var_dump($fullReport);
    }
} catch (InvalidUserKeyException $e) {
    echo 'Bad user key';
} catch (NotFoundException $e) {
    echo 'No data found <br>';
    echo 'For more information read server message below: <br>';
    echo sprintf(
        "StatusSesji:%s\nKomunikatKod:%s\nKomunikatTresc:%s\n",
        $gus->getSessionStatus(),
        $gus->getMessageCode(),
        $gus->getMessage()
    );
}