我想獲取由AJAX請求生成的數據。在這個頁面http://www.fipe.org.br/web/index.asp?p=51&aspx=/web/indices/veiculos/default.aspx有一些html選擇。當用戶點擊第一個(Marca)時,第二個填滿。我想獲得這些數據。PHP Curl - 獲取使用AJAX生成的數據
這是我的代碼:
<?php
$curl = curl_init();
$postData = array('ddlAnoValor' => 0,
'ddlMarca' => 1,
'ddlModelo' => 0,
'ddlTabelaReferencia' => 123,
'txtCodFipe' => '');
$result = null;
$httpResponse = null;
curl_setopt($curl, CURLOPT_URL, 'http://www.fipe.org.br/web/indices/veiculos/default.aspx?p=51');
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, 'http://www.fipe.org.br/web/indices/veiculos/introducao.aspx');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($curl);
$httpResponse = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if($httpResponse == '404') {
throw new exception('This page doesn\'t exists.');
}
echo $result;
curl_close($curl);
?>
頁請求頭
Host: www.fipe.org.br
User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.13) Gecko/20100916 Iceweasel/3.5.13 (like Firefox/3.5.13)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
X-MicrosoftAjax: Delta=true
Cache-Control: no-cache, no-cache
Content-Type: application/x-www-form-urlencoded; charset=utf-8
Referer: http://www.fipe.org.br/web/indices/veiculos/default.aspx?p=51
Content-Length: 9415
Cookie: __utma=106123796.1351303072.1287075522.1287075522.1287075522.1; __utmb=106123796; __utmc=106123796; __utmz=106123796.1287075522.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); ASPSESSIONIDAADQDQRD=EKBEJHEDKCIOAAHNFFMLGMKO
Pragma: no-cache
但我總是得到形式的結果。我試圖設置cookie,但cookies.txt文件始終爲空。我不知道這個cookie是否需要。 cookies.txt有777權限。我究竟做錯了什麼?謝謝。
您應該使用[LiveHTTPHeaders](https://addons.mozilla.org/en-US/firefox/addon/3829/)或Firebug之類的東西來找出正在進行的AJAX調用的具體內容(即:URL ,請求方法等)並使用cURL複製。 – NullUserException 2010-10-14 17:07:58
我用螢火蟲做過。我不知道什麼是錯的。謝謝。 – thomas 2010-10-14 17:11:06
似乎不尋常的是,表單會向自己發送一個AJAX請求到同一個URL,但我無法檢查(在公共計算機上我無法安裝任何東西) – NullUserException 2010-10-14 17:20:05