我正在为巴西的航班开发航班监控软件,我在从网站成功接收航班数据方面遇到了一些麻烦。它在ASP.NET中,我相信在实际显示数据之前必须有多个请求被填满。
我已经获得了特定于ASP.NET的变量(如EVENTVALIDATION和VIEWSTATE)并发送了多个请求(一个发送机场,另一个发送帖子),但它仍然是禁止的。
您选择机场和航空公司的页面为http://voos.infraero.gov.br/voos/index.aspx,但数据显示在http://voos.infraero.gov.br/voos/index_2.aspx。
代码未优化,因为我仍在调试中。提前感谢您的任何帮助。
<?php
// Sets the server variables for this script
ini_set("max_execution_time", 0);
ini_set("error_reporting", E_ALL);
ini_set("default_charset", "utf-8");
// Sets the cookie
$cookie = dirname(__FILE__)."/cookies/cookiejar.txt";
$fp = fopen($cookie, "w");
fclose($fp);
// Slices the string to the the viewstate value
function GetViewState($result)
{
$doc = new DOMDocument();
$doc->loadHTML($result);
$nodes = $doc->getElementsByTagName('input');
for($i = 0; $i < $nodes->length; $i++)
{
if ($nodes->item($i)->getAttribute('name') == "__VIEWSTATE")
$viewstate = $nodes->item($i)->getAttribute('value');
}
return $viewstate;
}
// Slices the string to the the eventvalidation value
function GetEventValidation($result)
{
$doc = new DOMDocument();
$doc->loadHTML($result);
$nodes = $doc->getElementsByTagName('input');
for($i = 0; $i < $nodes->length; $i++)
{
if ($nodes->item($i)->getAttribute('name') == "__EVENTVALIDATION")
$eventvalidation = $nodes->item($i)->getAttribute('value');
}
return $eventvalidation;
}
// Sends a request only to retrieve EVENTVALIDATION and VIEWSTATE values
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://voos.infraero.gov.br/voos/index.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_REFERER, "http://voos.infraero.gov.br/voos/index.aspx");
curl_setopt($ch, CURLOPT_VERBOSE, '1');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
echo $result = curl_exec($ch);
$viewstate = GetViewState($result);
$eventvalidation = GetEventValidation($result);
/////////////////////////////////////////////////////////////////
// Sends the received VIEWSTATE and EVENTVALIDATION values, as well as the airport (SBCF)
$ch = curl_init();
$postfields = "ScriptManager1=update|aero_companias_aeroportos";
$postfields .= "&__EVENTTARGET=aero_companias_aeroportos";
$postfields .= "&__EVENTARGUMENT=";
$postfields .= "&__LASTFOCUS=";
$postfields .= "&__VIEWSTATE=".$viewstate;
$postfields .= "&__EVENTVALIDATION=".$eventvalidation;
$postfields .= "&pode_pesquisar_aeroporto=S";
$postfields .= "&aero_companias_aeroportos=SBCF";
$postfields .= "&txt_num_voo=";
$postfields .= "&txt_water_box_ClientState=";
//echo $postfields;
curl_setopt($ch, CURLOPT_URL, "http://voos.infraero.gov.br/voos/index.aspx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_REFERER, "http://voos.infraero.gov.br/voos/index.aspx");
curl_setopt($ch, CURLOPT_VERBOSE, '1');
echo $result = curl_exec($ch);
$viewstate = GetViewState($result);
$eventvalidation = GetEventValidation($result);
/////////////////////////////////////////////////////////////////
// Sends the same as the previous request, plus the form
$postfields = "ScriptManager1=update|btnPesquisar";
$postfields .= "&__EVENTTARGET=";
$postfields .= "&__EVENTARGUMENT=";
$postfields .= "&__LASTFOCUS=";
$postfields .= "&__VIEWSTATE=".$viewstate;
$postfields .= "&__EVENTVALIDATION=".$eventvalidation;
$postfields .= "&pode_pesquisar_aeroporto=S";
$postfields .= "&aero_companias_aeroportos=SBCF";
$postfields .= "&aero_companias_companias=Selecione%20uma%20Companhia%20A%C3%A9rea";
$postfields .= "&txt_num_voo=";
$postfields .= "&txt_water_box_ClientState=";
$postfields .= "&btnPesquisar=Consultar%20Voos";
//echo $postfields;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://voos.infraero.gov.br/voos/index.aspx");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_REFERER, "http://voos.infraero.gov.br/voos/index.aspx");
curl_setopt($ch, CURLOPT_VERBOSE, '1');
echo $result = curl_exec($ch);
$viewstate = GetViewState($result);
$eventvalidation = GetEventValidation($result);
/////////////////////////////////////////////////////////////////
// Shows the page with the results
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://voos.infraero.gov.br/voos/index_2.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt($ch, CURLOPT_REFERER, "http://voos.infraero.gov.br/voos/index.aspx");
curl_setopt($ch, CURLOPT_VERBOSE, '1');
echo $result = curl_exec($ch);
/////////////////////////////////////////////////////////////////
?>