PHP Curl - 获取使用 AJAX 生成的数据

发布于 2024-09-27 04:09:16 字数 2319 浏览 4 评论 0原文

我想获取 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权限。我做错了什么?谢谢。

I want to get data generated by an AJAX request. In this page http://www.fipe.org.br/web/index.asp?p=51&aspx=/web/indices/veiculos/default.aspx there are some html selects. When the user click on the first one (Marca), the second one is filled. I want to get this data.

This is my code:

<?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);
?>

Page request header

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

But I always get the form as result. I've tried to set cookie but cookies.txt file is always empty. I don't know if this cookie is required. cookies.txt has 777 permission. What am I doing wrong? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

树深时见影 2024-10-04 04:09:17

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

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

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

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

If you look at the post variables (use the net panel on firebug to do this) when using the form on the site, you will see that it contains some variables which you are not submitting with your PHP code, such as _VIEWSTATE and _EVENTVALIDATION.

I guess that these relate to the session established by the browser when displaying the form, and I further guess that if these and their related variables are not present then the server will return the full page HTML including the form.

You could try to simulate these variables, but I suspect you are doomed to fail.

Ideally you should contact the site and ask them how you can retrieve the information you are looking for. Perhaps they have a webservice which exposes it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文