bir.phpNowy plik, 39 linii
1<?php2declare(strict_types=1);3 4const BIR_URL = 'https://wyszukiwarkaregontest.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc';5const BIR_NS = 'http://CIS/BIR/PUBL/2014/07';6 7function bir(string $action, string $body, ?string $sid = null): string8{9 $envelope = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"'10 . ' xmlns:ns="' . BIR_NS . '" xmlns:dat="' . BIR_NS . '/DataContract">'11 . '<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">'12 . '<wsa:To>' . BIR_URL . '</wsa:To>'13 . '<wsa:Action>' . BIR_NS . '/IUslugaBIRzewnPubl/' . $action . '</wsa:Action>'14 . '</soap:Header><soap:Body>' . $body . '</soap:Body></soap:Envelope>';15 16 $headers = ['Content-Type: application/soap+xml; charset=utf-8'];17 if ($sid !== null) {18 $headers[] = 'sid: ' . $sid;19 }20 $curl = curl_init(BIR_URL);21 curl_setopt_array($curl, [22 CURLOPT_POST => true,23 CURLOPT_POSTFIELDS => $envelope,24 CURLOPT_HTTPHEADER => $headers,25 CURLOPT_RETURNTRANSFER => true,26 CURLOPT_TIMEOUT => 30,27 ]);28 $raw = (string) curl_exec($curl);29 curl_close($curl);30 31 32 if (!preg_match('/<s:Envelope.*<\/s:Envelope>/s', $raw, $match)) {33 throw new RuntimeException('BIR nie zwrócił koperty SOAP');34 }35 $xml = new SimpleXMLElement($match[0]);36 $xml->registerXPathNamespace('b', BIR_NS);37 $result = $xml->xpath('//b:' . $action . 'Result');38 return $result ? (string) $result[0] : '';39}
bir.phpZmiana w pliku: 4 linie
1function bir_login(string $key): string2{3 return bir('Zaloguj',4 '<ns:Zaloguj><ns:pKluczUzytkownika>' . $key . '</ns:pKluczUzytkownika></ns:Zaloguj>');5}6 7$sid = bir_login(getenv('GUS_BIR_KEY') ?: 'abcde12345abcde12345');
bir.phpZmiana w pliku: 11 linii
1function bir_find_by_nip(string $sid, string $nip): ?SimpleXMLElement2{3 $result = bir('DaneSzukajPodmioty',4 '<ns:DaneSzukajPodmioty><ns:pParametryWyszukiwania>'5 . '<dat:Nip>' . $nip . '</dat:Nip>'6 . '</ns:pParametryWyszukiwania></ns:DaneSzukajPodmioty>', $sid);7 if ($result === '') {8 return null; 9 }10 $root = new SimpleXMLElement($result); 11 return isset($root->dane->Regon) ? $root->dane : null; 12}13 14$company = bir_find_by_nip($sid, '5261040828');15echo $company->Nazwa, ' | REGON ', $company->Regon, ' | typ ', $company->Typ, "\n";
bir.phpZmiana w pliku: 9 linii
1function bir_report_name(SimpleXMLElement $company): ?string2{3 return match ((string) $company->Typ . '/' . (string) $company->SilosID) {4 'P/6' => 'BIR12OsPrawna',5 'F/1' => 'BIR12OsFizycznaDzialalnoscCeidg',6 'F/2' => 'BIR12OsFizycznaDzialalnoscRolnicza',7 'F/3' => 'BIR12OsFizycznaDzialalnoscPozostala',8 'F/4' => 'BIR12OsFizycznaDzialalnoscSkreslonaDo20141108',9 default => null,10 };11}
bir.phpZmiana w pliku: 7 linii
1function bir_report(string $sid, string $regon, string $report): SimpleXMLElement2{3 $result = bir('DanePobierzPelnyRaport',4 '<ns:DanePobierzPelnyRaport><ns:pRegon>' . $regon . '</ns:pRegon>'5 . '<ns:pNazwaRaportu>' . $report . '</ns:pNazwaRaportu></ns:DanePobierzPelnyRaport>', $sid);6 return new SimpleXMLElement($result);7}8 9$full = bir_report($sid, (string) $company->Regon, bir_report_name($company))->dane;10echo $full->praw_nazwa, ', ', $full->praw_adSiedzKodPocztowy, ' ', $full->praw_adSiedzMiejscowosc_Nazwa;
bir.phpZmiana w pliku: 2 linie
1bir('Wyloguj',2 '<ns:Wyloguj><ns:pIdentyfikatorSesji>' . $sid . '</ns:pIdentyfikatorSesji></ns:Wyloguj>', $sid);
company.phpNowy plik, 22 linie
1function company_by_nip(PDO $db, string $nip): ?array2{3 $row = $db->prepare('SELECT data, fetched_at FROM gus_cache WHERE nip = ?');4 $row->execute([$nip]);5 $hit = $row->fetch(PDO::FETCH_ASSOC);6 if ($hit && strtotime($hit['fetched_at']) > time() - 86400) {7 return json_decode($hit['data'], true); 8 }9 $sid = bir_login(getenv('GUS_BIR_KEY'));10 try {11 $company = bir_find_by_nip($sid, $nip);12 } finally {13 bir('Wyloguj', '<ns:Wyloguj><ns:pIdentyfikatorSesji>' . $sid . '</ns:pIdentyfikatorSesji></ns:Wyloguj>', $sid);14 }15 if ($company === null) {16 return null;17 }18 $data = json_decode(json_encode($company), true);19 $db->prepare('REPLACE INTO gus_cache (nip, data, fetched_at) VALUES (?, ?, NOW())')20 ->execute([$nip, json_encode($data, JSON_UNESCAPED_UNICODE)]);21 return $data;22}