I'm trying to get information from a web form. The website is a public search form, where you can look up cars in Denmark and get some basic information about the car: Here is a link to the website DMR, where you can try to input the license plate 'AZ87303'. (Remember to select the radiobutton Registreringsnummer before you submit).
If you look at the source code, there is a hidden field called 'dmrFormToken'
with a random token. I extracted this token, and I include this in the POST request. Here is my current code to simulate a POST request in PHP with this cUrl script.
<?php
// Source: http://simplehtmldom.sourceforge.net/
include('inc/simple_html_dom.php');
/* Get Form Token */
$formUrl = "https://motorregister.skat.dk/dmr-front/appmanager/skat/dmr?_nfpb=true&_nfpb=true&_pageLabel=vis_koeretoej_side&_nfls=false";
$dmrFormToken = file_get_html($formUrl)->find('input[name=dmrFormToken]')[0]->value;
//extract data from the post
//set POST variables
$url = "https://motorregister.skat.dk/dmr-front/appmanager/skat/dmr?_nfpb=true&_windowLabel=kerne_vis_koeretoej&kerne_vis_koeretoej_actionOverride=%2Fdk%2Fskat%2Fdmr%2Ffront%2Fportlets%2Fkoeretoej%2Fnested%2FfremsoegKoeretoej%2Fsearch&_pageLabel=vis_koeretoej_side";
$fields = array(
urlencode('dmrFormToken') => urlencode($dmrFormToken),
urlencode('kerne_vis_koeretoejwlw-radio_button_group_key:{actionForm.soegekriterie}') => urlencode("REGISTRERINGSNUMMER"),
urlencode('kerne_vis_koeretoej{actionForm.soegeord}') => urlencode("AZ87303"),
urlencode('kerne_vis_koeretoejactionOverride:search') => urlencode("Søg")
);
$fields_string = "";
//url-ify the data for the POST
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
$fields_string = rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//print response
echo $result;
?>
I get the following error:
This document you requested has moved temporarily.
It's now at https://motorregister.skat.dk/dmr-front/appmanager/skat/dmr?_nfls=false&_nfpb=true&_pageLabel=vis_koeretoej_side.
I've downloaded the LiveHttpHeaders extensions for FireFox, which allows me to follow the request. Here is the result
Here is the POST request content from LiveHttpHeaders and from the cUrl script above:
LiveHTTPHeaders:
dmrFormToken=5l1bq8pllh54kciahlu8lml3ou&kerne_vis_koeretoejwlw-radio_button_group_key%3A%7BactionForm.soegekriterie%7D=REGISTRERINGSNUMMER&kerne_vis_koeretoej%7BactionForm.soegeord%7D=AZ87303&kerne_vis_koeretoejactionOverride%3Asearch=S%C3%B8g
cUrl Script: // echo $fields_string;
dmrFormToken=s6qghv0013ch7u3pcsineao6lu&kerne_vis_koeretoejwlw-radio_button_group_key%3A%7BactionForm.soegekriterie%7D=REGISTRERINGSNUMMER&kerne_vis_koeretoej%7BactionForm.soegeord%7D=AZ87303&kerne_vis_koeretoejactionOverride%3Asearch=S%C3%B8g
How can I receive the source code from the target URL with my custom POST request
The question thread suggested below doesnt solve the problem. I've able to make a POST request to my own test page, but once I try it on the https://motorregister.skat.dk/ it doesnt work.