【问题标题】:PHP Curl - Get data generated with AJAXPHP Curl - 获取使用 AJAX 生成的数据
【发布时间】:2011-04-25 12:56:22
【问题描述】:

我想获取 AJAX 请求生成的数据。在此页面http://www.fipe.org.br/web/index.asp?p=51&aspx=/web/indices/veiculos/default.aspx 中有一些 html 选择。当用户点击第一个(Marca)时,第二个被填充。我想得到这个数据。

这是我的代码:

<?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 或 Firebug 之类的东西来找出正在进行的 AJAX 调用的确切内容(即:URL、请求方法等)并使用 cURL 重现它。
  • 我是用萤火虫做的。我不知道怎么了。谢谢。
  • 表单会向与自身相同的 URL 发送 AJAX 请求似乎很不寻常,但我无法检查(在无法安装任何东西的公共计算机上)
  • 我已经用请求标头信息更新了问题。谢谢。

标签: php html ajax select curl


【解决方案1】:

如果您在网站上使用表单时查看 post 变量(使用 firebug 上的 net 面板来执行此操作),您会看到它包含一些您未使用 PHP 代码提交的变量,例如 _VIEWSTATE和 _EVENTVALIDATION。

我猜这些与浏览器在显示表单时建立的会话有关,我进一步猜测如果这些及其相关变量不存在,那么服务器将返回包括表单在内的完整页面 HTML。

您可以尝试模拟这些变量,但我怀疑您注定要失败。

理想情况下,您应该联系该网站并询问他们如何检索您正在寻找的信息。也许他们有一个暴露它的网络服务?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多