Curl Post - 服务器不返回响应DVLA刮ASPx [英] Curl Post - server not returning response DVLA scraping ASPx

查看:228
本文介绍了Curl Post - 服务器不返回响应DVLA刮ASPx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用下面的cURL脚本来使用DVLA网站上的数据来协助mysite上的数据实现。非常抱歉,我无法指向代码的原始来源,因为我从这个网站获得它,并未注明来源。



  function hhb_curl_exec($ ch,$ url){static $ hhb_curl_domainCache =; // $ hhb_curl_domainCache =& $ this-> hhb_curl_domainCache; // $ ch =& $ this-> curlh; if(!is_resource($ ch)|| get_resource_type($ ch)!=='curl'){throw new InvalidArgumentException('$ ch must be a curl handle! } if(!is_string($ url)){throw new InvalidArgumentException('$ url must be a string!'); } $ tmpvar =; if(parse_url($ url,PHP_URL_HOST)=== null){if(substr($ url,0,1)!=='/'){$ url = $ hhb_curl_domainCache。 '/'。 $ url; } else {$ url = $ hhb_curl_domainCache。 $ url; }}; curl_setopt($ ch,CURLOPT_URL,$ url); $ html = curl_exec($ ch); if(curl_errno($ ch)){throw new Exception('Curl error(curl_errno ='。curl_errno($ ch)。')on url'。var_export($ url,true)。':'。curl_error($ ch) ); // echo'Curl error:'。 curl_error($ ch); } if($ html ===''&& 203!=($ tmpvar = curl_getinfo($ ch,CURLINFO_HTTP_CODE))/ * 203是成功,但没有输出.. * /){throw new Exception 'curl没有为'。var_export($ url,true)。'返回任何东西',但是HTTP_RESPONSE_CODE为'var_export($ tmpvar,true)); }; //记住curl(通常)自动跟随Location:http重定向.. $ hhb_curl_domainCache = parse_url(curl_getinfo($ ch,CURLINFO_EFFECTIVE_URL),PHP_URL_HOST); return $ html;} function hhb_curl_exec2($ ch,$ url,& $ returnHeaders = array(),& $ returnCookies = array(),& $ verboseDebugInfo =){$ returnHeaders = array $ returnCookies = array(); $ verboseDebugInfo =; if(!is_resource($ ch)|| get_resource_type($ ch)!=='curl'){throw new InvalidArgumentException('$ ch must be a curl handle! } if(!is_string($ url)){throw new InvalidArgumentException('$ url must be a string!'); } $ verbosefileh = tmpfile(); $ verbosefile = stream_get_meta_data($ verbosefileh); $ verbosefile = $ verbosefile ['uri']; curl_setopt($ ch,CURLOPT_VERBOSE,1); curl_setopt($ ch,CURLOPT_STDERR,$ verbosefileh); curl_setopt($ ch,CURLOPT_HEADER,1); $ html = hhb_curl_exec($ ch,$ url); $ verboseDebugInfo = file_get_contents($ verbosefile); curl_setopt($ ch,CURLOPT_STDERR,NULL); fclose($ verbosefileh); unset($ verbosefile,$ verbosefileh); $ headers = array(); $ crlf =\x0d\x0a; $ thepos = strpos($ html,$ crlf。$ crlf,0); $ headersString = substr($ html,0,$ thepos); $ headerArr = explode($ crlf,$ headersString); $ returnHeaders = $ headerArr; unset($ headerString,$ headerArr); $ htmlBody = substr($ html,$ thepos + 4); //应该工作在utf8 / ascii标题... utf32? not so sure .. unset($ html); //我真的希望有一个更好的方式来获取COOKIES ..好的悲伤这看起来丑陋.. //至少它的测试,似乎工作完美... $ grabCookieName = function($ str,& $ len){ $ len = 0; $ ret =; $ i = 0; for($ i = 0; $ i \013\014'// * / if(stripos($ header,Set-Cookie:)!== 0){continue; / ** /} $ header = trim(substr($ header,strlen(Set-Cookie:))); $ len = 0; while(strlen($ header)> 0){$ cookiename = $ grabCookieName($ header,$ len); $ returnCookies [$ cookiename] =''; $ header = substr($ header,$ len + 1); //也删除= if(strlen($ header)< 1){break; }; $ thepos = strpos($ header,';'); if($ thepos === false){//这个Set-Cookie中的最后一个cookie。 $ returnCookies [$ cookiename] = urldecode($ header);打破; } $ returnCookies [$ cookiename] = urldecode(substr($ header,0,$ thepos)); $ header = trim(substr($ header,$ thepos + 1)); //也删除; }} unset($ header,$ cookiename,$ thepos); return $ htmlBody;} ############################################# ################# function hhb_curl_init($ custom_options_array = array()){if(empty($ custom_options_array)){$ custom_options_array = array();我想要一个字符串(url)或NULL //至少我想允许NULL aswell:/} if(!is_array($ custom_options_array)){throw new InvalidArgumentException('$ custom_options_array必须是数组!'); }; $ options_array = array(CURLOPT_AUTOREFERER => true,CURLOPT_BINARYTRANSFER => true,CURLOPT_COOKIESESSION => true,CURLOPT_FOLLOWLOCATION => true,CURLOPT_FORBID_REUSE => false,CURLOPT_HTTPGET => true,CURLOPT_RETURNTRANSFER => true,CURLOPT_SSL_VERIFYPEER => ; false,CURLOPT_CONNECTTIMEOUT => 10,CURLOPT_TIMEOUT => 11,CURLOPT_ENCODING =>//CURLOPT_REFERER=>'example.org', //CURLOPT_USERAGENT=&m;Mozilla/5.0(Windows NT 6.1; WOW64; rv:36.0)Gecko / 20100101 Firefox / 36.0'); if(!array_key_exists(CURLOPT_COOKIEFILE,$ custom_options_array)){//这样做只有条件,因为tmpfile()调用.. static $ curl_cookiefiles_arr = array(); // https://bugs.php.net/bug.php?id=66014的变通方法$ curl_cookiefiles_arr [] = $ options_array [CURLOPT_COOKIEFILE] = tmpfile(); $ options_array [CURLOPT_COOKIEFILE] = stream_get_meta_data($ options_array [CURLOPT_COOKIEFILE]); $ options_array [CURLOPT_COOKIEFILE] = $ options_array [CURLOPT_COOKIEFILE] ['uri']; } //我们不能使用array_merge(),因为它如何处理整数键,它会/可能导致损坏foreach($ custom_options_array作为$ key => $ val){$ options_array [$ key] = $ val; } unset($ key,$ val,$ custom_options_array); $ curl = curl_init(); curl_setopt_array($ curl,$ options_array); return $ curl;} // ------------------------------------------- ----- ////注册和VRM必须是有效的,并按照DVLA格式,使它不会工作!$ registration_number ='PK07LVD'; //必须是一个有效的VRM才能得到正确的响应$ vehicle_maker ='BMW'; // Make must matchVRM hold with DVLA,并以与DVLA列表相同的格式输入到站点$ ch = hhb_curl_init(); $ debugHeaders = array(); $ debugCookies = array(); $ debugRequest =''; $ html = hhb_curl_exec2 $ ch,'https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest); //首先做一个空请求获取一个会话ID和cookie和奇怪的VIEWSTATE stuff ... $ domd = @ DOMDocument :: loadHTML($ html); assert(is_object($ domd)); $ __ VIEWSTATE = $ domd-> getElementById('__ VIEWSTATE') - > getAttribute('value') ; $ __ VIEWSTATEGENERATOR = $ domd-> getElementById('__ VIEWSTATEGENERATOR') - > getAttribute('value'); $ __ EVENTVALIDATION = $ domd-> getElementById('__ EVENTVALIDATION') - > getAttribute('value'); / var_dump('__ VIEWSTATE:',$ __ VIEWSTATE,'__ VIEWSTATEGENERATOR:',$ __ VIEWSTATEGENERATOR,'__ EVENTVALIDATION:',$ __ EVENTVALIDATION,'headers:',$ debugHeaders,'cookies:',$ debugCookies,'html:',$ html ,'request:',$ debugRequest,'domd:',$ domd); //现在获取POST stuffcurl_setopt_array($ ch,array(CURLOPT_POST => true,CURLOPT_POSTFIELDS => http_build_query(array('__ LASTFOCUS' >'','__ EVENTTARGET'=>'','__ VIEWSTATE'=> $ __ VIEWSTATE,'__ VIEWSTATEGENERATOR'=> $ __ VIEWSTATEGENERATOR,'__ EVENTVALIDATION'=> $ __ EVENTVALIDATION,'ctl00 $ MainContent $ txtSearchVrm'=> ;'$ registration_number','ctl00 $ MainContent $ MakeTextBox'=>'$ vehicles_maker','ctl00 $ MainContent $ txtV5CDocumentReferenceNumber'=>'','ctl00 $ MainContent $ butSearch'=& )); $ html = hhb_curl_exec2($ ch,'https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest); // var_dump('headers:', $ debugHeaders,'cookies:',$ debugCookies,'html:',$ html,'request:',$ debugRequest); echo $ html;?>  

/ div>



由于某些原因,上一周的某些事情发生了变化,我没有得到在$ html中返回的结果,



查询表单发送的变量是:



Array([__EVENTTARGET] => [__EVENTARGUMENT] => [__VIEWSTATE] => / wEPDwUKMTQ2ODczMjQwMA8WAh4NU2VydmljZUhlYWRlcjL8BAABAAAA ///// wEAAAAAAAAADAIAAABBRG1zIFNoYXJlZCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAACVEbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uSGVhZGVyCQAAABNjb252ZXJzYXRpb25JZEZpZWxkD29yaWdpbkRhdGVGaWVsZBJhcHBsaWNhdGlvbkNERmllbGQOY2hhbm5lbENERmllbGQOY29udGFjdElkRmllbGQNZXZlbnRGbGdGaWVsZBJzZXJ2aWNlVHlwZUNERmllbGQPbGFuZ3VhZ2VDREZpZWxkDGVuZFVzZXJGaWVsZAEAAQEAAAQEBA0JAS5EbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uU2VydmljZVR5cGVDb2RlAgAAACtEbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uTGFuZ3VhZ2VDb2RlAgAAACZEbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uRW5kVXNlcgIAAAACAAAACswmEsba79OIBgMAAAALRVZMX1NjcmVlbnMGBAAAAAhEVkxBX1dFQvPsDXUAAAAAAQX7 //// /// LkRtc1NoYXJlZC5EYXRhQ29udHJhY3RzLkNvbW1vbi5TZXJ2aWNlVHlwZUNvZGUBAAAAB3ZhbHVlX18ACAIAAAACAAAABfr 8rRG1zU2hhcmVkLkRhdGFDb250cmFjdHMuQ29tbW9uLkxhbmd1YWdlQ29kZQEAAAAHdmFsdWVfXwAIAgAAAAAAAAAKCxYCZg9kFgICAQ9kFgRmDxYCHgRUZXh0BQ9WZWhpY2xlIGVucXVpcnlkAgIPDxYCHgtOYXZpZ2F0ZVVybAUvaHR0cHM6Ly93aC5zbmFwc3VydmV5cy5jb20vcy5hc3A / az0xNDcwMjMwNjQwNTRkZBgBBSFjdGwwMCRNYWluQ29udGVudCRtdlZlaGljbGVTZWFyY2gPD2RmZAl8wP9HdAZERXThmPjkY7mMhrt6 [__VIEWSTATEGENERATOR] => CA0B0334 [__EVENTVALIDATION] => / wEdAAec98WnAVQeQUdqU6NI4oVRBOwywjxOOgpEYFN2beEgnftoCCZcWJSqSRLD / FKuxxkI0x5r4gPeKgWgSNWptTEWInv2PXI3Jzdn3U6eHDG4Qb7lltCXTdtnDbitYujbDJI0GQSIMiv32DreL6oRbYpQ8QTO8WJr3q5Y80Jf5PzdZW5VzdA = [ctl00 $ $日程地址搜索Maincontent txtSearchVrm] => pk07lvd [ctl00 $ MainContent $ MakeTextBox] => bmw [ctl00 $ MainContent $ txtV5CDocumentReferenceNumber] => [ctl00 $ MainContent $ butSearch] =>搜索)



当我从我的服务器远程运行表单表单将变量POST到 https://vehicleenquiry.service.gov.uk/ ,我会得到所需的结果被返回,但显然通过Goverment网站显示在浏览器上。



我正在寻找有关可能发生或可能发生错误的帮助与代码。拉我的头发试图调查每个阶段为什么,当我运行cURL编码,我不是得到的结果返回$ html,而只是最初的查询形式。



我会感谢任何遗产。我知道有一个类似的职位,但关于调查这个问题已经做了什么的细节不多。由于是新的网站说,我couldnt答复。



// -------------------从原始代码添加09/10/16 21:46 - ---------------- //



此代码构成另一个函数的一部分。



// ----------------------- ----关于输出---------- //



在某种意义上,这个代码的工作方式是$ html总是输出来自车辆的响应



问题是远程站点是( https://vehicleenquiry.service.gov.uk )似乎没有处理POST数据并返回查询的结果。所有返回的是最初的查询表单,好像您没有输入任何表单数据。






包含具有车辆详细信息,税务,电动状态的响应:



而是cURL命令响应返回的查询表单。

解决方案

第一次错误

 'ctl00 $ MainContent $ txtSearchVrm'=>'$ registration_number',
'ctl00 $ MainContent $ MakeTextBox'=>'$ vehicles_maker',

应为:

  ctl00 $ MainContent $ txtSearchVrm'=> $ registration_number,
'ctl00 $ MainContent $ MakeTextBox'=> $ vehicle_maker,

$ b b

第二次错误

  $ html = hhb_curl_exec2($ ch, https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest); 

应为:

  $ html = hhb_curl_exec2($ ch,'https://vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest); 

整个代码

 <?php 
function dvlascrape($ send_array)
{

$ make_trimmed = $ send_array ['2'] ;
$ vrm_strip = $ send_array ['0'];



函数hhb_curl_exec($ ch,$ url)
{
static $ hhb_curl_domainCache =;
// $ hhb_curl_domainCache =& $ this-> hhb_curl_domainCache;
// $ ch =& $ this-> curlh;
if(!is_resource($ ch)|| get_resource_type($ ch)!=='curl'){
throw new InvalidArgumentException('$ ch必须是curl handle!
}
if(!is_string($ url)){
throw new InvalidArgumentException('$ url must be a string!');
}

$ tmpvar =;
if(parse_url($ url,PHP_URL_HOST)=== null){
if(substr($ url,0,1)!=='/'){
$ url = $ hhb_curl_domainCache。 '/'。 $ url;
} else {
$ url = $ hhb_curl_domainCache。 $ url;
}
}
;

curl_setopt($ ch,CURLOPT_URL,$ url);
$ html = curl_exec($ ch);
if(curl_errno($ ch)){
在url'。var_export($ url,true)上抛出新的异常('Curl error(curl_errno ='。curl_errno($ ch)。 :'。curl_error($ ch));
// echo'Curl错误:'。 curl_error($ ch);
}
if($ html ===''&& 203!=($ tmpvar = curl_getinfo($ ch,CURLINFO_HTTP_CODE))/ * 203是成功, * /){
throw new Exception('Curl没有为'。var_export($ url,true)'返回',但是HTTP_RESPONSE_CODE为'var_export($ tmpvar,true)
}
;
//记住curl(通常)自动跟随位置:http重定向..
$ hhb_curl_domainCache = parse_url(curl_getinfo($ ch,CURLINFO_EFFECTIVE_URL),PHP_URL_HOST);
return $ html;
}
function hhb_curl_exec2($ ch,$ url,& $ returnHeaders = array(),& $ returnCookies = array(),& $ verboseDebugInfo =)
{
$ returnHeaders = array();
$ returnCookies = array();
$ verboseDebugInfo =;
if(!is_resource($ ch)|| get_resource_type($ ch)!=='curl'){
throw new InvalidArgumentException('$ ch必须是curl handle!
}
if(!is_string($ url)){
throw new InvalidArgumentException('$ url must be a string!');
}
$ verbosefileh = tmpfile();
$ verbosefile = stream_get_meta_data($ verbosefileh);
$ verbosefile = $ verbosefile ['uri'];
curl_setopt($ ch,CURLOPT_VERBOSE,1);
curl_setopt($ ch,CURLOPT_STDERR,$ verbosefileh);
curl_setopt($ ch,CURLOPT_HEADER,1);
$ html = hhb_curl_exec($ ch,$ url);
$ verboseDebugInfo = file_get_contents($ verbosefile);
curl_setopt($ ch,CURLOPT_STDERR,NULL);
fclose($ verbosefileh);
unset($ verbosefile,$ verbosefileh);
$ headers = array();
$ crlf =\x0d\x0a;
$ thepos = strpos($ html,$ crlf。$ crlf,0);
$ headersString = substr($ html,0,$ thepos);
$ headerArr = explode($ crlf,$ headersString);
$ returnHeaders = $ headerArr;
unset($ headerString,$ headerArr);
$ htmlBody = substr($ html,$ thepos + 4); //应该工作在utf8 / ascii标题... utf32?不太确定..
unset($ html);
//我真的希望有一个更好的方式来获取COOKIES ..好的悲伤这看起来丑陋..
//至少它的测试,似乎工作完美...
$ grabCookieName = function($ str,& $ len)
{
$ len = 0;
$ ret =;
$ i = 0;
for($ i = 0; $ i ++ $ len;
if($ str [$ i] ===''){
continue;
}
if($ str [$ i] ==='='){
- $ len;
break;
}
$ ret。= $ str [$ i];
}
return urldecode($ ret);
};
foreach($ returnHeaders as $ header){
// Set-Cookie:crlfcoookielol = crlf + is%0D%0A + and + newline + is +%0D%0A + and + semicolon + is%3B + and + not + sure + what + else
/ * Set-Cookie:ci_spill = a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22305d3d67b8016ca9661c3b032d4319df%22%3Bs% 3A10%3A%22ip_address%22%3Bs%3A14%3A%2285.164.158.128%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A109%3A%22Mozilla%2F5.0 +%28Windows + NT + 6.1%3B + WOW64%29 + AppleWebKit%2F537.36 +%28KHTML%2C + like + Gecko%29 + Chrome%2F43.0.2357.132 + Safari%2F537.36%22%3Bs%3A13%3A%22last_activity%22%3Bi% 3A1436874639%3B%7Dcab1dd09f4eca466660e8a767856d013; expires = Tue,14-Jul-2015 13:50:39 GMT; path = /
Set-Cookie:sessionToken = abc123; Expires = Wed,09 Jun 2021 10:18:14 GMT;
// Cookie名称不能包含以下任何'= ,; \t\r\\\
\013\014'
//
* /
if(stripos($ header,Set-Cookie:)!== 0 ){
continue;
/ ** /
}
$ header = trim(substr($ header,strlen(Set-Cookie:)));
$ len = 0;
while(strlen($ header)> 0){
$ cookiename = $ grabCookieName($ header,$ len);
$ returnCookies [$ cookiename] ='';
$ header = substr($ header,$ len + 1); //也删除=
if(strlen($ header)< 1){
break;
}
;
$ thepos = strpos($ header,';');
if($ thepos === false){//这个Set-Cookie中的最后一个cookie。
$ returnCookies [$ cookiename] = urldecode($ header);
break;
}
$ returnCookies [$ cookiename] = urldecode(substr($ header,0,$ thepos));
$ header = trim(substr($ header,$ thepos + 1)); //也删除;
}
}
unset($ header,$ cookiename,$ thepos);
return $ htmlBody;
}

##################################### ################################
function hhb_curl_init($ custom_options_array = array())
{
if( empty($ custom_options_array)){
$ custom_options_array = array();
//对此感觉很糟糕。argv [1] curl_init想要一个字符串(url)或NULL
//至少我想允许NULL aswell:/
}
if(!is_array($ custom_options_array)){
throw new InvalidArgumentException('$ custom_options_array必须是数组!
}
;
$ options_array = array(
CURLOPT_AUTOREFERER => true,
CURLOPT_BINARYTRANSFER => true,
CURLOPT_COOKIESESSION => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_FORBID_REUSE => false,
CURLOPT_HTTPGET => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 11,
CURLOPT_ENCODING =>
//CURLOPT_REFERER=>'example.org',
//CURLOPT_USERAGENT=>'Mozilla/5.0 Windows NT 6.1; WOW64; rv:36.0)Gecko / 20100101 Firefox / 36.0'
);
if(!array_key_exists(CURLOPT_COOKIEFILE,$ custom_options_array)){
//只有有条件地这么做,因为tmpfile()调用..
static $ curl_cookiefiles_arr = array // https://bugs.php.net/bug.php?id=66014
的解决方法curl_cookiefiles_arr [] = $ options_array [CURLOPT_COOKIEFILE] = tmpfile();
$ options_array [CURLOPT_COOKIEFILE] = stream_get_meta_data($ options_array [CURLOPT_COOKIEFILE]);
$ options_array [CURLOPT_COOKIEFILE] = $ options_array [CURLOPT_COOKIEFILE] ['uri'];

}
//我们不能使用array_merge(),因为它处理整数键,会导致损坏
foreach($ custom_options_array as $ key = > $ val){
$ options_array [$ key] = $ val;
}
unset($ key,$ val,$ custom_options_array);
$ curl = curl_init();
curl_setopt_array($ curl,$ options_array);
return $ curl;
}




// ----------------------- ------------------------- //

//注册和VRM必须有效,并按照DVLA格式为otehrwise这不会工作!

$ registration_number ='PK07LVD'; //必须是有效的VRM才能得到正确的响应
$ vehicle_maker ='BMW'; // Make must matchVRM hold with DVLA,并以与DVLA列表相同的格式输入到站点


$ ch = hhb_curl_init();

$ debugHeaders = array();
$ debugCookies = array();
$ debugRequest ='';

$ html = hhb_curl_exec2($ ch,'https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);
//首先做一个空请求来获取会话ID和cookie以及奇怪的VIEWSTATE内容...
$ domd = @ DOMDocument :: loadHTML($ html);
assert(is_object($ domd));
$ __ VIEWSTATE = $ domd-> getElementById('__ VIEWSTATE') - > getAttribute('value');
$ __ VIEWSTATEGENERATOR = $ domd-> getElementById('__ VIEWSTATEGENERATOR') - > getAttribute('value');
$ __ EVENTVALIDATION = $ domd-> getElementById('__ EVENTVALIDATION') - > getAttribute('value');

// var_dump('__ VIEWSTATE:',$ __ VIEWSTATE,'__ VIEWSTATEGENERATOR:',$ __ VIEWSTATEGENERATOR,'__ EVENTVALIDATION:',$ __ EVENTVALIDATION,'headers:',$ debugHeaders,'cookies:',$ debugCookies,'html:',$ html,'request:',$ debugRequest,'domd:',$ domd);
var_dump($ __ VIEWSTATE);
//现在获取POST材料
curl_setopt_array($ ch,array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query(array(
' __LASTFOCUS'=>',
'__EVENTTARGET'=>',
'__EVENTARGUMENT'=>'',
'__VIEWSTATE'=> $ __ VIEWSTATE,
'__VIEWSTATEGENERATOR'=> $ __ VIEWSTATEGENERATOR,
'__EVENTVALIDATION'=> $ __ EVENTVALIDATION,
'ctl00 $ MainContent $ txtSearchVrm'=> $ registration_number,
'ctl00 $ MainContent $ MakeTextBox '=> $ vehicle_maker,
'ctl00 $ MainContent $ txtV5CDocumentReferenceNumber'=>'',
'ctl00 $ MainContent $ butSearch'=>'搜索',
))
));
var_dump(
array(
'__LASTFOCUS'=>'',
'__EVENTTARGET'=>'',
'__EVENTARGUMENT'=> ,
'__VIEWSTATE'=> $ __ VIEWSTATE,
'__VIEWSTATEGENERATOR'=> $ __ VIEWSTATEGENERATOR,
'__EVENTVALIDATION'=> $ __ EVENTVALIDATION,
'ctl00 $ MainContent $ txtSearchVrm '=> $ registration_number,
'ctl00 $ MainContent $ MakeTextBox'=> $ vehicle_maker,
'ctl00 $ MainContent $ txtV5CDocumentReferenceNumber'=>'',
'ctl00 $ MainContent $ butSearch'=>'Search',
));

$ html = hhb_curl_exec2($ ch,'https://vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);
// var_dump(curl_error($ ch));
// var_dump('headers:',$ debugHeaders,'cookies:',$ debugCookies,'html:',$ html,'request:',$ debugRequest);

return $ html;






}

var_dump(dvlascrape(array('','',' ')));
?>


I have previously been using the following cURL script to use data from the DVLA website to assist with data implemenation on mysite. I apologise that I cannot point to the original source of the code as I got it from this site and failed to note the source.

function hhb_curl_exec($ch, $url)
{
    static $hhb_curl_domainCache = "";
    //$hhb_curl_domainCache=&$this->hhb_curl_domainCache;
    //$ch=&$this->curlh;
    if (!is_resource($ch) || get_resource_type($ch) !== 'curl') {
        throw new InvalidArgumentException('$ch must be a curl handle!');
    }
    if (!is_string($url)) {
        throw new InvalidArgumentException('$url must be a string!');
    }

    $tmpvar = "";
    if (parse_url($url, PHP_URL_HOST) === null) {
        if (substr($url, 0, 1) !== '/') {
            $url = $hhb_curl_domainCache . '/' . $url;
        } else {
            $url = $hhb_curl_domainCache . $url;
        }
    }
    ;

    curl_setopt($ch, CURLOPT_URL, $url);
    $html = curl_exec($ch);
    if (curl_errno($ch)) {
        throw new Exception('Curl error (curl_errno=' . curl_errno($ch) . ') on url ' . var_export($url, true) . ': ' . curl_error($ch));
        // echo 'Curl error: ' . curl_error($ch);
    }
    if ($html === '' && 203 != ($tmpvar = curl_getinfo($ch, CURLINFO_HTTP_CODE)) /*203 is "success, but no output"..*/ ) {
        throw new Exception('Curl returned nothing for ' . var_export($url, true) . ' but HTTP_RESPONSE_CODE was ' . var_export($tmpvar, true));
    }
    ;
    //remember that curl (usually) auto-follows the "Location: " http redirects..
    $hhb_curl_domainCache = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), PHP_URL_HOST);
    return $html;
}
function hhb_curl_exec2($ch, $url, &$returnHeaders = array(), &$returnCookies = array(), &$verboseDebugInfo = "")
{
    $returnHeaders    = array();
    $returnCookies    = array();
    $verboseDebugInfo = "";
    if (!is_resource($ch) || get_resource_type($ch) !== 'curl') {
        throw new InvalidArgumentException('$ch must be a curl handle!');
    }
    if (!is_string($url)) {
        throw new InvalidArgumentException('$url must be a string!');
    }
    $verbosefileh = tmpfile();
    $verbosefile  = stream_get_meta_data($verbosefileh);
    $verbosefile  = $verbosefile['uri'];
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_STDERR, $verbosefileh);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $html             = hhb_curl_exec($ch, $url);
    $verboseDebugInfo = file_get_contents($verbosefile);
    curl_setopt($ch, CURLOPT_STDERR, NULL);
    fclose($verbosefileh);
    unset($verbosefile, $verbosefileh);
    $headers       = array();
    $crlf          = "\x0d\x0a";
    $thepos        = strpos($html, $crlf . $crlf, 0);
    $headersString = substr($html, 0, $thepos);
    $headerArr     = explode($crlf, $headersString);
    $returnHeaders = $headerArr;
    unset($headersString, $headerArr);
    $htmlBody = substr($html, $thepos + 4); //should work on utf8/ascii headers... utf32? not so sure..
    unset($html);
    //I REALLY HOPE THERE EXIST A BETTER WAY TO GET COOKIES.. good grief this looks ugly..
    //at least it's tested and seems to work perfectly...
    $grabCookieName = function($str,&$len)
    {
        $len=0;
        $ret = "";
        $i   = 0;
        for ($i = 0; $i < strlen($str); ++$i) {
            ++$len;
            if ($str[$i] === ' ') {
                continue;
            }
            if ($str[$i] === '=') {
                --$len;
                break;
            }
            $ret .= $str[$i];
        }
        return urldecode($ret);
    };
    foreach ($returnHeaders as $header) {
        //Set-Cookie: crlfcoookielol=crlf+is%0D%0A+and+newline+is+%0D%0A+and+semicolon+is%3B+and+not+sure+what+else
        /*Set-Cookie:ci_spill=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22305d3d67b8016ca9661c3b032d4319df%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%2285.164.158.128%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A109%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F43.0.2357.132+Safari%2F537.36%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1436874639%3B%7Dcab1dd09f4eca466660e8a767856d013; expires=Tue, 14-Jul-2015 13:50:39 GMT; path=/
        Set-Cookie: sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT;
        //Cookie names cannot contain any of the following '=,; \t\r\n\013\014'
        //
        */
        if (stripos($header, "Set-Cookie:") !== 0) {
            continue;
            /**/
        }
        $header = trim(substr($header, strlen("Set-Cookie:")));
        $len=0;
        while (strlen($header) > 0) {
            $cookiename                 = $grabCookieName($header,$len);
            $returnCookies[$cookiename] = '';
            $header                     = substr($header, $len + 1); //also remove the = 
            if (strlen($header) < 1) {
                break;
            }
            ;
            $thepos = strpos($header, ';');
            if ($thepos === false) { //last cookie in this Set-Cookie.
                $returnCookies[$cookiename] = urldecode($header);
                break;
            }
            $returnCookies[$cookiename] = urldecode(substr($header, 0, $thepos));
            $header                     = trim(substr($header, $thepos + 1)); //also remove the ;
        }
    }
    unset($header, $cookiename, $thepos);
    return $htmlBody;
}

##############################################################
function hhb_curl_init($custom_options_array = array())
{
    if (empty($custom_options_array)) {
        $custom_options_array = array();
        //i feel kinda bad about this.. argv[1] of curl_init wants a string(url), or NULL
        //at least i want to allow NULL aswell :/
    }
    if (!is_array($custom_options_array)) {
        throw new InvalidArgumentException('$custom_options_array must be an array!');
    }
    ;
    $options_array = array(
        CURLOPT_AUTOREFERER => true,
        CURLOPT_BINARYTRANSFER => true,
        CURLOPT_COOKIESESSION => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_FORBID_REUSE => false,
        CURLOPT_HTTPGET => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 11,
        CURLOPT_ENCODING => ""
        //CURLOPT_REFERER=>'example.org',
        //CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0'
    );
    if (!array_key_exists(CURLOPT_COOKIEFILE, $custom_options_array)) {
        //do this only conditionally because tmpfile() call..
        static $curl_cookiefiles_arr = array(); //workaround for https://bugs.php.net/bug.php?id=66014
        $curl_cookiefiles_arr[]            = $options_array[CURLOPT_COOKIEFILE] = tmpfile();
        $options_array[CURLOPT_COOKIEFILE] = stream_get_meta_data($options_array[CURLOPT_COOKIEFILE]);
        $options_array[CURLOPT_COOKIEFILE] = $options_array[CURLOPT_COOKIEFILE]['uri'];

    }
    //we can't use array_merge() because of how it handles integer-keys, it would/could cause corruption
    foreach ($custom_options_array as $key => $val) {
        $options_array[$key] = $val;
    }
    unset($key, $val, $custom_options_array);
    $curl = curl_init();
    curl_setopt_array($curl, $options_array);
    return $curl;
}




//------------------------------------------------//

//Registration and VRM have to be valid and make as per the DVLA format for makes otehrwise this wont work!

$registration_number = 'PK07LVD'; // must be a valid VRM to get the correct response
$vehicle_maker ='BMW'; //Make must matchVRM held with DVLA and entered in same format as DVLA list on site


$ch=hhb_curl_init();

$debugHeaders=array();
$debugCookies=array();
$debugRequest='';

$html=hhb_curl_exec2($ch,'https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);
//first do an empty request to get a session id and cookies and the weird VIEWSTATE stuff...
$domd=@DOMDocument::loadHTML($html);
assert(is_object($domd));
$__VIEWSTATE=$domd->getElementById('__VIEWSTATE')->getAttribute('value');
$__VIEWSTATEGENERATOR=$domd->getElementById('__VIEWSTATEGENERATOR')->getAttribute('value');
$__EVENTVALIDATION=$domd->getElementById('__EVENTVALIDATION')->getAttribute('value');

//var_dump('__VIEWSTATE:',$__VIEWSTATE,'__VIEWSTATEGENERATOR:',$__VIEWSTATEGENERATOR,'__EVENTVALIDATION:',$__EVENTVALIDATION,'headers:',$debugHeaders,'cookies:',$debugCookies,'html:',$html,'request:',$debugRequest,'domd:',$domd);

//now to get the POST stuff
curl_setopt_array($ch,array(
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query(array(
'__LASTFOCUS'=>'',
'__EVENTTARGET'=>'',
'__VIEWSTATE'=>$__VIEWSTATE,
'__VIEWSTATEGENERATOR'=>$__VIEWSTATEGENERATOR,
'__EVENTVALIDATION'=>$__EVENTVALIDATION,
'ctl00$MainContent$txtSearchVrm'=>'$registration_number',
'ctl00$MainContent$MakeTextBox'=>'$vehicle_maker',
'ctl00$MainContent$txtV5CDocumentReferenceNumber'=>'',
'ctl00$MainContent$butSearch'=>'Search',
))
));


$html=hhb_curl_exec2($ch,'https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);
//var_dump('headers:',$debugHeaders,'cookies:',$debugCookies,'html:',$html,'request:',$debugRequest);

echo $html;

?>

For some reason over the last week something has changed and I am not getting a result being returned in $html, all that is happening is the form to enter the Vehicle registration and Make is being displayed.

The variables that the enquiry form is sending are:

Array
(
    [__EVENTTARGET] => 
    [__EVENTARGUMENT] => 
    [__VIEWSTATE] => /wEPDwUKMTQ2ODczMjQwMA8WAh4NU2VydmljZUhlYWRlcjL8BAABAAAA/////wEAAAAAAAAADAIAAABBRG1zIFNoYXJlZCwgVmVyc2lvbj0xLjAuMC4wLCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPW51bGwFAQAAACVEbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uSGVhZGVyCQAAABNjb252ZXJzYXRpb25JZEZpZWxkD29yaWdpbkRhdGVGaWVsZBJhcHBsaWNhdGlvbkNERmllbGQOY2hhbm5lbENERmllbGQOY29udGFjdElkRmllbGQNZXZlbnRGbGdGaWVsZBJzZXJ2aWNlVHlwZUNERmllbGQPbGFuZ3VhZ2VDREZpZWxkDGVuZFVzZXJGaWVsZAEAAQEAAAQEBA0JAS5EbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uU2VydmljZVR5cGVDb2RlAgAAACtEbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uTGFuZ3VhZ2VDb2RlAgAAACZEbXNTaGFyZWQuRGF0YUNvbnRyYWN0cy5Db21tb24uRW5kVXNlcgIAAAACAAAACswmEsba79OIBgMAAAALRVZMX1NjcmVlbnMGBAAAAAhEVkxBX1dFQvPsDXUAAAAAAQX7////LkRtc1NoYXJlZC5EYXRhQ29udHJhY3RzLkNvbW1vbi5TZXJ2aWNlVHlwZUNvZGUBAAAAB3ZhbHVlX18ACAIAAAACAAAABfr///8rRG1zU2hhcmVkLkRhdGFDb250cmFjdHMuQ29tbW9uLkxhbmd1YWdlQ29kZQEAAAAHdmFsdWVfXwAIAgAAAAAAAAAKCxYCZg9kFgICAQ9kFgRmDxYCHgRUZXh0BQ9WZWhpY2xlIGVucXVpcnlkAgIPDxYCHgtOYXZpZ2F0ZVVybAUvaHR0cHM6Ly93aC5zbmFwc3VydmV5cy5jb20vcy5hc3A/az0xNDcwMjMwNjQwNTRkZBgBBSFjdGwwMCRNYWluQ29udGVudCRtdlZlaGljbGVTZWFyY2gPD2RmZAl8wP9HdAZERXThmPjkY7mMhrt6
    [__VIEWSTATEGENERATOR] => CA0B0334
    [__EVENTVALIDATION] => /wEdAAec98WnAVQeQUdqU6NI4oVRBOwywjxOOgpEYFN2beEgnftoCCZcWJSqSRLD/FKuxxkI0x5r4gPeKgWgSNWptTEWInv2PXI3Jzdn3U6eHDG4Qb7lltCXTdtnDbitYujbDJI0GQSIMiv32DreL6oRbYpQ8QTO8WJr3q5Y80Jf5PzdZW5VzdA=
    [ctl00$MainContent$txtSearchVrm] => pk07lvd
    [ctl00$MainContent$MakeTextBox] => bmw
    [ctl00$MainContent$txtV5CDocumentReferenceNumber] => 
    [ctl00$MainContent$butSearch] => Search
)

When I run the form remotely from my server and have the form POST the variables to https://vehicleenquiry.service.gov.uk/, I do get the required result being returned, but obviously being displayed on the browers through the Goverment website.

I am looking for help as to what is possibly going on, or has gone wrong with the code. Pulling my hair out trying to investigate each stage as to why, when I run the cURL coding, I am not getting the result returned in $html, but just the initial enquiry form.

I would be grateful for any assitance. I know there was a similar post, but with less detail as to what had been done to investigate the issue. Due to being new the site said I couldnt reply.

//-------------------Edit from original code added 09/10/16 21:46 -----------------//

This code forms part of another function. I have since removed the overriding function for clarity.

//--------------------------- Regarding output----------//

In a sense this code does work as $html always outputs a response from the vehicle enquiry service..

The issue is that the remote site is (https://vehicleenquiry.service.gov.uk) does not seem to be processing the POST data and returning the result of the query. All that is returned is the initial enquiry form as though you had not entered any form data.


I would expect $html to contain a response with vehicle details, tax, mot status:

Instead the cURL command responds with the enquiry form being returned.

解决方案

First mistake:

'ctl00$MainContent$txtSearchVrm'=>'$registration_number',
'ctl00$MainContent$MakeTextBox'=>'$vehicle_maker',

Should be:

'ctl00$MainContent$txtSearchVrm'=>$registration_number,
'ctl00$MainContent$MakeTextBox'=>$vehicle_maker,

Second mistake

$html=hhb_curl_exec2($ch,'https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);

Should be:

$html=hhb_curl_exec2($ch,'https://vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);

Whole code:

<?php
function dvlascrape($send_array)
{

$make_trimmed=$send_array['2'];
$vrm_strip=$send_array['0'];



function hhb_curl_exec($ch, $url)
{
    static $hhb_curl_domainCache = "";
    //$hhb_curl_domainCache=&$this->hhb_curl_domainCache;
    //$ch=&$this->curlh;
    if (!is_resource($ch) || get_resource_type($ch) !== 'curl') {
        throw new InvalidArgumentException('$ch must be a curl handle!');
    }
    if (!is_string($url)) {
        throw new InvalidArgumentException('$url must be a string!');
    }

    $tmpvar = "";
    if (parse_url($url, PHP_URL_HOST) === null) {
        if (substr($url, 0, 1) !== '/') {
            $url = $hhb_curl_domainCache . '/' . $url;
        } else {
            $url = $hhb_curl_domainCache . $url;
        }
    }
    ;

    curl_setopt($ch, CURLOPT_URL, $url);
    $html = curl_exec($ch);
    if (curl_errno($ch)) {
        throw new Exception('Curl error (curl_errno=' . curl_errno($ch) . ') on url ' . var_export($url, true) . ': ' . curl_error($ch));
        // echo 'Curl error: ' . curl_error($ch);
    }
    if ($html === '' && 203 != ($tmpvar = curl_getinfo($ch, CURLINFO_HTTP_CODE)) /*203 is "success, but no output"..*/ ) {
        throw new Exception('Curl returned nothing for ' . var_export($url, true) . ' but HTTP_RESPONSE_CODE was ' . var_export($tmpvar, true));
    }
    ;
    //remember that curl (usually) auto-follows the "Location: " http redirects..
    $hhb_curl_domainCache = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL), PHP_URL_HOST);
    return $html;
}
function hhb_curl_exec2($ch, $url, &$returnHeaders = array(), &$returnCookies = array(), &$verboseDebugInfo = "")
{
    $returnHeaders    = array();
    $returnCookies    = array();
    $verboseDebugInfo = "";
    if (!is_resource($ch) || get_resource_type($ch) !== 'curl') {
        throw new InvalidArgumentException('$ch must be a curl handle!');
    }
    if (!is_string($url)) {
        throw new InvalidArgumentException('$url must be a string!');
    }
    $verbosefileh = tmpfile();
    $verbosefile  = stream_get_meta_data($verbosefileh);
    $verbosefile  = $verbosefile['uri'];
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_STDERR, $verbosefileh);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $html             = hhb_curl_exec($ch, $url);
    $verboseDebugInfo = file_get_contents($verbosefile);
    curl_setopt($ch, CURLOPT_STDERR, NULL);
    fclose($verbosefileh);
    unset($verbosefile, $verbosefileh);
    $headers       = array();
    $crlf          = "\x0d\x0a";
    $thepos        = strpos($html, $crlf . $crlf, 0);
    $headersString = substr($html, 0, $thepos);
    $headerArr     = explode($crlf, $headersString);
    $returnHeaders = $headerArr;
    unset($headersString, $headerArr);
    $htmlBody = substr($html, $thepos + 4); //should work on utf8/ascii headers... utf32? not so sure..
    unset($html);
    //I REALLY HOPE THERE EXIST A BETTER WAY TO GET COOKIES.. good grief this looks ugly..
    //at least it's tested and seems to work perfectly...
    $grabCookieName = function($str,&$len)
    {
        $len=0;
        $ret = "";
        $i   = 0;
        for ($i = 0; $i < strlen($str); ++$i) {
            ++$len;
            if ($str[$i] === ' ') {
                continue;
            }
            if ($str[$i] === '=') {
                --$len;
                break;
            }
            $ret .= $str[$i];
        }
        return urldecode($ret);
    };
    foreach ($returnHeaders as $header) {
        //Set-Cookie: crlfcoookielol=crlf+is%0D%0A+and+newline+is+%0D%0A+and+semicolon+is%3B+and+not+sure+what+else
        /*Set-Cookie:ci_spill=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22305d3d67b8016ca9661c3b032d4319df%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%2285.164.158.128%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A109%3A%22Mozilla%2F5.0+%28Windows+NT+6.1%3B+WOW64%29+AppleWebKit%2F537.36+%28KHTML%2C+like+Gecko%29+Chrome%2F43.0.2357.132+Safari%2F537.36%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1436874639%3B%7Dcab1dd09f4eca466660e8a767856d013; expires=Tue, 14-Jul-2015 13:50:39 GMT; path=/
        Set-Cookie: sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT;
        //Cookie names cannot contain any of the following '=,; \t\r\n\013\014'
        //
        */
        if (stripos($header, "Set-Cookie:") !== 0) {
            continue;
            /**/
        }
        $header = trim(substr($header, strlen("Set-Cookie:")));
        $len=0;
        while (strlen($header) > 0) {
            $cookiename                 = $grabCookieName($header,$len);
            $returnCookies[$cookiename] = '';
            $header                     = substr($header, $len + 1); //also remove the =
            if (strlen($header) < 1) {
                break;
            }
            ;
            $thepos = strpos($header, ';');
            if ($thepos === false) { //last cookie in this Set-Cookie.
                $returnCookies[$cookiename] = urldecode($header);
                break;
            }
            $returnCookies[$cookiename] = urldecode(substr($header, 0, $thepos));
            $header                     = trim(substr($header, $thepos + 1)); //also remove the ;
        }
    }
    unset($header, $cookiename, $thepos);
    return $htmlBody;
}

##############################################################
function hhb_curl_init($custom_options_array = array())
{
    if (empty($custom_options_array)) {
        $custom_options_array = array();
        //i feel kinda bad about this.. argv[1] of curl_init wants a string(url), or NULL
        //at least i want to allow NULL aswell :/
    }
    if (!is_array($custom_options_array)) {
        throw new InvalidArgumentException('$custom_options_array must be an array!');
    }
    ;
    $options_array = array(
        CURLOPT_AUTOREFERER => true,
        CURLOPT_BINARYTRANSFER => true,
        CURLOPT_COOKIESESSION => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_FORBID_REUSE => false,
        CURLOPT_HTTPGET => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 11,
        CURLOPT_ENCODING => ""
        //CURLOPT_REFERER=>'example.org',
        //CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0'
    );
    if (!array_key_exists(CURLOPT_COOKIEFILE, $custom_options_array)) {
        //do this only conditionally because tmpfile() call..
        static $curl_cookiefiles_arr = array(); //workaround for https://bugs.php.net/bug.php?id=66014
        $curl_cookiefiles_arr[]            = $options_array[CURLOPT_COOKIEFILE] = tmpfile();
        $options_array[CURLOPT_COOKIEFILE] = stream_get_meta_data($options_array[CURLOPT_COOKIEFILE]);
        $options_array[CURLOPT_COOKIEFILE] = $options_array[CURLOPT_COOKIEFILE]['uri'];

    }
    //we can't use array_merge() because of how it handles integer-keys, it would/could cause corruption
    foreach ($custom_options_array as $key => $val) {
        $options_array[$key] = $val;
    }
    unset($key, $val, $custom_options_array);
    $curl = curl_init();
    curl_setopt_array($curl, $options_array);
    return $curl;
}




//------------------------------------------------//

//Registration and VRM have to be valid and make as per the DVLA format for makes otehrwise this wont work!

$registration_number = 'PK07LVD'; // must be a valid VRM to get the correct response
$vehicle_maker ='BMW'; //Make must matchVRM held with DVLA and entered in same format as DVLA list on site


$ch=hhb_curl_init();

$debugHeaders=array();
$debugCookies=array();
$debugRequest='';

$html=hhb_curl_exec2($ch,'https://www.vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);
//first do an empty request to get a session id and cookies and the weird VIEWSTATE stuff...
$domd=@DOMDocument::loadHTML($html);
assert(is_object($domd));
$__VIEWSTATE=$domd->getElementById('__VIEWSTATE')->getAttribute('value');
$__VIEWSTATEGENERATOR=$domd->getElementById('__VIEWSTATEGENERATOR')->getAttribute('value');
$__EVENTVALIDATION=$domd->getElementById('__EVENTVALIDATION')->getAttribute('value');

//var_dump('__VIEWSTATE:',$__VIEWSTATE,'__VIEWSTATEGENERATOR:',$__VIEWSTATEGENERATOR,'__EVENTVALIDATION:',$__EVENTVALIDATION,'headers:',$debugHeaders,'cookies:',$debugCookies,'html:',$html,'request:',$debugRequest,'domd:',$domd);
var_dump($__VIEWSTATE);
//now to get the POST stuff
curl_setopt_array($ch,array(
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>http_build_query(array(
'__LASTFOCUS'=>'',
'__EVENTTARGET'=>'',
'__EVENTARGUMENT'=>'',
'__VIEWSTATE'=>$__VIEWSTATE,
'__VIEWSTATEGENERATOR'=>$__VIEWSTATEGENERATOR,
'__EVENTVALIDATION'=>$__EVENTVALIDATION,
'ctl00$MainContent$txtSearchVrm'=>$registration_number,
'ctl00$MainContent$MakeTextBox'=>$vehicle_maker,
'ctl00$MainContent$txtV5CDocumentReferenceNumber'=>'',
'ctl00$MainContent$butSearch'=>'Search',
))
));
var_dump(
array(
'__LASTFOCUS'=>'',
'__EVENTTARGET'=>'',
'__EVENTARGUMENT'=>'',
'__VIEWSTATE'=>$__VIEWSTATE,
'__VIEWSTATEGENERATOR'=>$__VIEWSTATEGENERATOR,
'__EVENTVALIDATION'=>$__EVENTVALIDATION,
'ctl00$MainContent$txtSearchVrm'=>$registration_number,
'ctl00$MainContent$MakeTextBox'=>$vehicle_maker,
'ctl00$MainContent$txtV5CDocumentReferenceNumber'=>'',
'ctl00$MainContent$butSearch'=>'Search',
));

$html=hhb_curl_exec2($ch,'https://vehicleenquiry.service.gov.uk/Default.aspx',$debugHeaders,$debugCookies,$debugRequest);
//var_dump(curl_error ( $ch )) ;
//var_dump('headers:',$debugHeaders,'cookies:',$debugCookies,'html:',$html,'request:',$debugRequest);

return $html;






}

var_dump(dvlascrape(array('', '', '')));
?>

这篇关于Curl Post - 服务器不返回响应DVLA刮ASPx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆