Ostrzegam, że kod nawet w połowie nie jest skończony: brakuje zupełnie obsługi błędów, logowania, a nawet parsowania otrzymanych danych. Wymaga pecl http. Do rozpoczęcia nowego requestu wymagany parametr ?reset
.
Klasa:
<?php
class NipRequest
{
const UNINITIALIZED_PHASE = 0;
const INITIALIZED_PHASE = 1;
const CAPTCHA_PHASE = 2;
const REQUEST_PHASE = 3;
const COMPLETED_PHASE = 4;
private $phase;
private $options;
private $cookies;
private $info;
public function __construct()
{
$this->phase = self::UNINITIALIZED_PHASE;
$this->options = array(
'referer' => 'http://www.stat.gov.pl/regon/',
'useragent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20130224 Firefox/21.0',
'headers' => array(
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' => 'Accept-Language=pl,en-us;q=0.7,en;q=0.3'
),
'compress' => true,
'redirect' => 3,
'connecttimeout' => 10,
'timeout' => 10
);
$this->cookies = array();
}
private function add_cookies($cookies)
{
$this->cookies = array_merge($this->cookies, $cookies);
}
private function update_cookies($response)
{
$headers = $response->getHeaders();
if(array_key_exists('Set-Cookie', $headers)) {
$cookies = http_parse_cookie(implode(';', $headers['Set-Cookie']));
$this->add_cookies($cookies->cookies);
}
}
public function get_phase()
{
return $this->phase;
}
public function initialize()
{
$request = new HttpRequest('http://www.stat.gov.pl/regon/', HttpRequest::METH_GET, $this->options);
$response = $request->send();
$this->update_cookies($response);
$this->phase = self::INITIALIZED_PHASE;
}
public function fetch_captcha()
{
if($this->phase < self::INITIALIZED_PHASE)
throw new Exception('Cannot get captcha until initialized phase');
if(!isset($this->captcha))
{
$request = new HttpRequest('http://www.stat.gov.pl/regon/Captcha.jpg?' . rand(1, 999), HttpRequest::METH_GET, $this->options);
$request->setCookies($this->cookies);
$response = $request->send();
$this->update_cookies($response);
$this->captcha = $request->getResponseBody();
}
$this->phase = self::CAPTCHA_PHASE;
return $this->captcha;
}
public function send_request($captcha, $nip)
{
if($this->phase != self::CAPTCHA_PHASE)
throw new Exception('Cannot send request unless in captcha phase');
$post_fields = array(
'queryTypeRBSet' => '1nip',
$captcha . '00' => '',
$captcha . '11' => $nip,
'verifCodeTF' => $captcha
);
$request = new HttpRequest('http://www.stat.gov.pl/regon/', HttpRequest::METH_POST, $this->options);
$this->add_cookies(array(
'lastCritIx' => '1',
'isAmbiguousAnswer' => 'false',
'toGenerNewVerifCode' => 'false',
'cCodeHistory' => $captcha,
'openingPageType' => '',
'focusedObjIdGlobal' => 'verifCodeTF',
'focusedObjCursorPos' => '5',
'lastCCode' => $captcha,
'criterion1TF' => $nip,
'browser' => 'FF-21.0',
'testCookie' => ''
));
$request->setPostFields($post_fields);
$request->setCookies($this->cookies);
$response = $request->send();
$this->update_cookies($response);
$this->phase = self::COMPLETED_PHASE;
$this->info = $request->getResponseBody();
}
public function get_response()
{
if($this->phase != self::COMPLETED_PHASE)
throw new Exception('Cannot get response until it\'s sent');
return $this->info;
}
}
Przykładowe użycie:
<?php
require 'NipRequest.class.php';
session_start();
if(!isset($_SESSION['request']) || isset($_GET['reset']))
$_SESSION['request'] = new NipRequest();
$request = &$_SESSION['request'];
if(isset($_GET['captcha'])) {
header('Content-type: image/jpeg');
print $request->fetch_captcha();
} else {
if($request->get_phase() < NipRequest::INITIALIZED_PHASE)
$request->initialize();
if($request->get_phase() > NipRequest::CAPTCHA_PHASE || (isset($_POST['captcha']) && isset($_POST['nip']))) {
if($request->get_phase() != NipRequest::COMPLETED_PHASE)
$request->send_request($_POST['captcha'], $_POST['nip']);
print $request->get_response();
} else {
?>
<img src="?captcha">
<form method="post">
captcha: <input type="text" name="captcha"><br>
nip: <input type="text" name="nip"><br>
<input type="submit" value="wyslij">
</form>
<?php
}
}