historiapojazdu.gov.pl pobranie raportu poprzez cURL

0

Cześć!
Próbuję napisać robota który z dostępnych danych pobierze mi raport z "historiapojazdu.gov.pl". Posiadam VIN, Numer rejestracyjny oraz datę pierwszej rejestracji na świecie.

$url = "https://historiapojazdu.gov.pl/";
  $vin = "WDD2462001J020815";
  $numer_rejestracyjny = "WSE9KU1";
  $data_pierwszej_rejestracji = date("2012-03-01");

  function curl($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);

    $content = curl_exec($curl);
    $resultUrl = explode('<form id="_historiapojazduportlet_WAR_historiapojazduportlet_:formularz" name="_historiapojazduportlet_WAR_historiapojazduportlet_:formularz" method="post" action="', $content);
    $resultUrl = explode('"', $resultUrl[1]);

    $resultViewState = explode('<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="', $content);
    $resultViewState = explode('"', $resultViewState[1]);

    $postUrl = $resultUrl[0];
    $viewState = $resultViewState[0];
  	return array("postURL"=>$postUrl, "javax.faces.ViewState"=>$viewState);
  }

  function sendPost($url, $numerRejestracyjny, $numerVIN, $dataPierwszejRejestracji) {
    $array = curl($url);
    $encodedUrl = "https://historiapojazdu.gov.pl/strona-glowna?p_p_id=historiapojazduportlet_WAR_historiapojazduportlet&p_p_lifecycle=2&p_p_state=normal&p_p_mode=view&p_p_cacheability=cacheLevelPage&p_p_col_id=column-1&p_p_col_count=1&_historiapojazduportlet_WAR_historiapojazduportlet__jsfBridgeAjax=true&_historiapojazduportlet_WAR_historiapojazduportlet__facesViewIdResource=%2Fviews%2Findex.xhtml&_historiapojazduportlet_WAR_historiapojazduportlet_javax.faces.ViewState=".urlencode($array['javax.faces.ViewState'])."&_historiapojazduportlet_WAR_historiapojazduportlet_:vin=".urlencode($numerVIN)."&_historiapojazduportlet_WAR_historiapojazduportlet_:data=".$dataPierwszejRejestracji."%2C&_historiapojazduportlet_WAR_historiapojazduportlet_amp;p_p_lifecycle=2&_historiapojazduportlet_WAR_historiapojazduportlet_:rej=".$numerRejestracyjny."&_historiapojazduportlet_WAR_historiapojazduportlet_:formularz=_historiapojazduportlet_WAR_historiapojazduportlet_%3Aformularz&_historiapojazduportlet_WAR_historiapojazduportlet_amp;p_p_mode=view&_historiapojazduportlet_WAR_historiapojazduportlet_amp;p_p_cacheability=cacheLevelPage&_historiapojazduportlet_WAR_historiapojazduportlet_let__jsfBridgeAjax=true&_historiapojazduportlet_WAR_historiapojazduportlet__facesViewIdRender=%2Fviews%2Findex.xhtml&_historiapojazduportlet_WAR_historiapojazduportlet_amp;p_p_col_id=column-1&_historiapojazduportlet_WAR_historiapojazduportlet_amp;p_p_state=normal&_historiapojazduportlet_WAR_historiapojazduportlet_amp;p_p_col_count=1&_historiapojazduportlet_WAR_historiapojazduportlet_let__facesViewIdResource=%2Fviews%2Findex.xhtml&_historiapojazduportlet_WAR_historiapojazduportlet_javax.faces.encodedURL=https%3A%2F%2Fhistoriapojazdu.gov.pl%2Fstrona-glowna%3Fp_p_id%3Dhistoriapojazduportlet_WAR_historiapojazduportlet";
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $array['postURL']);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
  	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "javax.faces.ViewState=".$array['javax.faces.ViewState']."&_historiapojazduportlet_WAR_historiapojazduportlet_:rej=".$numerRejestracyjny."&_historiapojazduportlet_WAR_historiapojazduportlet_:vin=".$numerVIN."&_historiapojazduportlet_WAR_historiapojazduportlet_:data=".$dataPierwszejRejestracji."&_historiapojazduportlet_WAR_historiapojazduportlet_:formularz=_historiapojazduportlet_WAR_historiapojazduportlet_:formularz&javax.faces.encodedURL=".$encodedUrl."&_historiapojazduportlet_WAR_historiapojazduportlet_:btnSprawdz=1");

    $content = curl_exec($curl);

  	return $content;
  }

  //zmienne do przesłania
    //method="post"
    //action=$postUrl
    //enctype="application/x-www-form-urlencoded"

  print sendPost($url, $numer_rejestracyjny, $vin, $data_pierwszej_rejestracji);

Niestety po wysłaniu danych poprzez POST ukazuję się BLANKPAGE :((

0

U mnie otrzymuję odpowiedź:

<partial-response><changes><update id="javax.faces.ViewState"><![CDATA[6399586131785473058:-402811014607184935]]></update></changes></partial-response>
0

@overcq: Tak też otrzymuję taką wiadomość... A powinno wyświetlić się: (https://4programmers.net/uploads/112501/vnHz3e25Q9jWzr8zII3Ql1Rwy6XdbIlsbgtz64cx.png))

0

@overcq: I nie mam pojęcią dlaczego nie udaje mi się pozyskać tych danych :((((

3

Teraz u mnie działa. (Tylko trzeba dopisać wymagane dane na początku.)

<?php
  $url = "https://historiapojazdu.gov.pl/";
  $vin = "";
  $numer_rejestracyjny = "";
  $data_pierwszej_rejestracji = '01.03.2012';

  function curl($curl, $url) {
    curl_setopt($curl, CURLOPT_URL, $url);

    $content = curl_exec($curl);
    $resultUrl = explode('<form id="_historiapojazduportlet_WAR_historiapojazduportlet_:formularz" name="_historiapojazduportlet_WAR_historiapojazduportlet_:formularz" method="post" action="', $content, 2);
    $resultUrl = explode('"', $resultUrl[1], 2)[0];

    $resultEncodedUrl = explode('<input type="hidden" name="javax.faces.encodedURL" value="', $content, 2);
    $resultEncodedUrl = explode('"', $resultEncodedUrl[1], 2)[0];

    $resultViewState = explode('<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="', $content);
    $resultViewState = explode('"', $resultViewState[1], 2)[0];
    return array("postURL"=>$resultUrl, "javax.faces.encodedURL"=>$resultEncodedUrl, "javax.faces.ViewState"=>$resultViewState);
  }

  function sendPost($url, $numerRejestracyjny, $numerVIN, $dataPierwszejRejestracji) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0');
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies');
    curl_setopt($curl, CURLOPT_COOKIELIST, 'ALL');
    $array = curl($curl, $url);

    curl_setopt($curl, CURLOPT_URL, $array['postURL']);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_AUTOREFERER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 
 urlencode('_historiapojazduportlet_WAR_historiapojazduportlet_:formularz').'='.urlencode('_historiapojazduportlet_WAR_historiapojazduportlet_:formularz').'&javax.faces.encodedURL='.urlencode(html_entity_decode($array['javax.faces.encodedURL'])).'&'.urlencode('_historiapojazduportlet_WAR_historiapojazduportlet_:rej').'='.urlencode($numerRejestracyjny).'&'.urlencode('_historiapojazduportlet_WAR_historiapojazduportlet_:vin').'='.urlencode($numerVIN).'&'.urlencode('_historiapojazduportlet_WAR_historiapojazduportlet_:data').'='.urlencode($dataPierwszejRejestracji).'&'.urlencode('_historiapojazduportlet_WAR_historiapojazduportlet_:btnSprawdz').'='.urlencode('Sprawdź pojazd »').'&javax.faces.ViewState='.urlencode($array['javax.faces.ViewState']));
    $content = curl_exec($curl);
    curl_close($curl);

    return $content;
  }

  //zmienne do przesłania
    //method="post"
    //action=$postUrl
    //enctype="application/x-www-form-urlencoded"

  print sendPost($url, $numer_rejestracyjny, $vin, $data_pierwszej_rejestracji);
?>
0

@overcq: Nie wiem jak mam Ci dziękować!!!! <3

1 użytkowników online, w tym zalogowanych: 0, gości: 1