我正在尝试从网络表单中获取信息。该网站是一个公共搜索表单,您可以在那里查找丹麦的汽车并获取有关汽车的一些基本信息:这是指向DMR网站的链接,您可以在此处输入车牌 'AZ87303'的。 (请记住在提交之前选择radiobutton Registreringsnummer 。)
如果您查看源代码,则会有一个名为'dmrFormToken'
的隐藏字段,其中包含一个随机令牌。我提取了这个令牌,并将其包含在POST请求中。这是我使用此cUrl脚本在PHP中模拟POST请求的当前代码。
<?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;
?>
我收到以下错误:
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.
我已经下载了FireFox的LiveHttpHeaders扩展程序,它允许我按照请求进行操作。结果如下
以下是来自LiveHttpHeaders和上面的cUrl脚本的POST请求内容:
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
如何使用自定义POST请求从目标网址接收源代码
下面提出的问题线程无法解决问题。我能够向自己的测试页面发出POST请求,但是一旦我在https://motorregister.skat.dk/上尝试它就无法工作。
答案 0 :(得分:0)
在你的卷曲请求中包含这一行:
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);