Skip to content

Commit 6d141bf

Browse files
author
johnzuk
committedFeb 16, 2015
Add get data support by NIP, REGON, KRS. Add first test and first example file.
1 parent e2e1b9c commit 6d141bf

File tree

5 files changed

+206
-2
lines changed

5 files changed

+206
-2
lines changed
 

‎examples/getFromNip.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
require_once '../vendor/autoload.php';
4+
5+
use Gus\GusApi\GusApi\GusApi;
6+
7+
session_start();
8+
9+
if(!isset($_SESSION['gus']) || isset($_GET['reset']))
10+
{
11+
$_SESSION['gus'] = new GusApi();
12+
$gus = &$_SESSION['gus'];
13+
}
14+
15+
$gus = &$_SESSION['gus'];
16+
17+
18+
if(!($gus->getCaptchaStatus()) && $gus == null)
19+
{
20+
$gus->getCaptcha();
21+
}
22+
23+
if($gus->getCaptchaStatus() || isset($_POST['captcha']))
24+
{
25+
if($gus->getCaptchaStatus() || $gus->checkCaptcha($_POST['captcha']))
26+
{
27+
var_dump($gus->getInfoByNip("5250010976"));
28+
var_dump($gus->getInfoByRegon("010344708"));
29+
}
30+
}
31+
32+
echo '<img src="captcha.jpeg">';
33+
echo '<form action="" method="POST">';
34+
echo '<input type="text" name="captcha" >';
35+
echo '<input type="submit" value="check">';
36+
echo '</form>';

‎phpunit.php

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
require_once "vendor/autoload.php";

‎phpunit.xml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<phpunit
2+
bootstrap="phpunit.php"
3+
colors="true">
4+
5+
<filter>
6+
<whitelist>
7+
<directory>src</directory>
8+
</whitelist>
9+
</filter>
10+
11+
<logging>
12+
<log type="coverage-html" target="build" charset="UTF-8" highlight="true" lowUpperBound="50" highLowerBound="80"></log>
13+
</logging>
14+
15+
</phpunit>

‎src/GusApi/GusApi.php

+152
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,51 @@ class GusApi
1818

1919
private $loginData = ["pKluczUzytkownika" => "aaaaaabbbbbcccccdddd"];
2020

21+
private $searchData = [
22+
'jestWojPowGmn' => true,
23+
'pParametryWyszukiwania' => [
24+
'AdsSymbolGminy' => null,
25+
'AdsSymbolMiejscowosci' => null,
26+
'AdsSymbolPowiatu' => null,
27+
'AdsSymbolUlicy' => null,
28+
'AdsSymbolWojewodztwa' => null,
29+
'Dzialalnosci' => null,
30+
'FormaPrawna' => null,
31+
'Krs' => null,
32+
'Krsy' => null,
33+
'NazwaPodmiotu' => null,
34+
'Nip' => null,
35+
'Nipy' => null,
36+
'NumerwRejestrzeLubEwidencji' => null,
37+
'OrganRejestrowy' => null,
38+
'PrzewazajacePKD' => false,
39+
'Regon' => null,
40+
'Regony14zn' => null,
41+
'Regony9zn' => null,
42+
'RodzajRejestru' => null
43+
]
44+
];
45+
2146
/**
2247
* @var string
2348
*/
2449
private $captchaFileName = "captcha.jpeg";
2550

2651
private $loginUrl = 'https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc/ajaxEndpoint/Zaloguj';
2752

53+
private $getCaptchaUrl = 'https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc/ajaxEndpoint/PobierzCaptcha';
54+
55+
private $checkCaptchaUrl = 'https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc/ajaxEndpoint/SprawdzCaptcha';
56+
57+
private $searchDataUrl = 'https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc/ajaxEndpoint/daneSzukaj';
58+
59+
private $getComplexDataUrl = 'https://wyszukiwarkaregon.stat.gov.pl/wsBIR/UslugaBIRzewnPubl.svc/ajaxEndpoint/DanePobierzPelnyRaport';
60+
61+
private $captchaStatus = false;
62+
2863
public function __construct()
2964
{
65+
$this->login();
3066
}
3167

3268
public function login()
@@ -43,5 +79,121 @@ public function login()
4379
$this->sid = $curl->response->d;
4480

4581
$curl->close();
82+
83+
return $this;
84+
}
85+
86+
public function getCaptcha()
87+
{
88+
$curl = new Curl();
89+
$curl->setHeader('Content-Type', 'application/json');
90+
$curl->setHeader('sid', $this->sid );
91+
$curl->post($this->getCaptchaUrl,'');
92+
93+
$image = fopen($this->captchaFileName,'w+');
94+
if(!$image)
95+
{
96+
throw new \Exception("NO file!");
97+
}
98+
99+
fwrite($image, base64_decode($curl->response->d));
100+
fclose($image);
101+
102+
$curl->close();
103+
104+
return $this;
105+
}
106+
107+
public function checkCaptcha($captcha)
108+
{
109+
$curl = new Curl();
110+
$curl->setHeader('Content-Type', 'application/json');
111+
$curl->setHeader('sid', $this->sid);
112+
$curl->post($this->checkCaptchaUrl, json_encode(array('pCaptcha'=>$captcha)));
113+
$curl->close();
114+
115+
$this->captchaStatus = (bool)$curl->response->d;
116+
return $this->captchaStatus;
117+
}
118+
119+
public function getInfoByNip($nip)
120+
{
121+
$basicInfo = $this->getBasicInfoByNip($nip);
122+
123+
return $this->getComplexData($basicInfo[0]->Regon);
124+
}
125+
126+
public function getInfoByRegon($regon)
127+
{
128+
$basicInfo = $this->getBasicInfoByRegon($regon);
129+
130+
return $this->getComplexData($basicInfo[0]->Regon);
131+
}
132+
133+
public function getInfoByKrs($krs)
134+
{
135+
$basicInfo = $this->getBasicInfoByKrs($krs);
136+
137+
return $this->getComplexData($basicInfo[0]->Regon);
138+
}
139+
140+
public function getBasicInfoByNip($nip)
141+
{
142+
$this->clearSearch();
143+
$this->searchData['pParametryWyszukiwania']['Nip'] = $nip;
144+
return $this->search();
145+
}
146+
147+
public function getBasicInfoByRegon($regon)
148+
{
149+
$this->clearSearch();
150+
$this->searchData['pParametryWyszukiwania']['Regon'] = $regon;
151+
return $this->search();
152+
}
153+
154+
public function getBasicInfoByKrs($krs)
155+
{
156+
$this->clearSearch();
157+
$this->searchData['pParametryWyszukiwania']['Krs'] = $krs;
158+
return $this->search();
159+
}
160+
161+
public function getCaptchaStatus()
162+
{
163+
return $this->captchaStatus;
164+
}
165+
166+
private function getComplexData($regon)
167+
{
168+
$searchData = [
169+
'pNazwaRaportu'=>'DaneRaportPrawnaPubl',
170+
'pRegon' => $regon,
171+
'pSilosID' => 0
172+
];
173+
174+
$curl = new Curl();
175+
$curl->setHeader('Content-Type', 'application/json');
176+
$curl->setHeader('sid', $this->sid);
177+
$curl->post($this->getComplexDataUrl, json_encode($searchData));
178+
$curl->close();
179+
180+
$response = json_decode($curl->response->d);
181+
return $response[0];
182+
}
183+
184+
private function clearSearch()
185+
{
186+
array_walk($this->searchData['pParametryWyszukiwania'],function($item,$key){ return null; });
187+
}
188+
189+
private function search()
190+
{
191+
$curl = new Curl();
192+
$curl->setHeader('Content-Type', 'application/json');
193+
$curl->setHeader('sid', $this->sid);
194+
$curl->post($this->searchDataUrl, json_encode($this->searchData));
195+
$curl->close();
196+
197+
return json_decode($curl->response->d);
46198
}
47199
}

‎tests/GusApiTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
require_once "../vendor/autoload.php";
4-
53
use Gus\GusApi\GusApi\GusApi;
64

75
class GusApiTest extends PHPUnit_Framework_TestCase

0 commit comments

Comments
 (0)
Please sign in to comment.