卷曲,未获取预期的页面数据
我正在尝试通过curl 连接到购物网站。
当通过 CURL 连接 http 时,我没有得到预期的数据返回,它说空篮子(带有 html 数据)。
这里的代码:
<?php
$agents = 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16';
$cookiesPath = "c:/wamp/www/Order/cookies";
$postArray['buy'] = "YTsdfjnsdjnfjsdnkjfnkjsdnfjknsdkfnksnkfn53534545=";
$postData = http_build_query($postArray);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agents);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "\Entrust.netSecureServerCertificationAuthority.crt");
curl_setopt($ch, CURLOPT_URL, "https://www.domain-site.com/checkout/checkout.php" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiesPath . "/cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiesPath . "/cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
echo $postResult;
?>
Cookies:
www.domain-site.com FALSE / FALSE 0 SECURESESSID e3psta9vbv4kribfi18jlqxxxx
www.domain-site.com FALSE /checkout/ FALSE 13052850xx basketStartTime 1305277xxx
我没有得到 html 的预期结果。
在使用 firebug 的 Firefox 上,这里是 http 请求:
GET /checkout/checkout.php?buy=YTsdfjnsdjnfjsdnkjfnkjsdnfjknsdkfnksnkfn53534545= HTTP/1.1
Host: www.domain-site.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: basketStartTime=1305276911; whoson=853-1303405947712; __utma=1.1209994793.1298996427.1305226205.1305276974.7; __utmz=1.1305276974.7.4.utmcsr=exxxxportal.com|utmccn=(referral)|utmcmd=referral|utmcct=/index.php; SECURESESSID=835qpldx8f1qikueau3s9oxxxx; __utmb=1.1.10.1305276974; __utmc=1
HTML 数据按预期返回,显示购物篮信息。
I am trying to connect to shopping backet website via curl.
When the http has been connected via CURL, I am not getting expected data return, it say empty basket (with html data).
Here the code:
<?php
$agents = 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16';
$cookiesPath = "c:/wamp/www/Order/cookies";
$postArray['buy'] = "YTsdfjnsdjnfjsdnkjfnkjsdnfjknsdkfnksnkfn53534545=";
$postData = http_build_query($postArray);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agents);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "\Entrust.netSecureServerCertificationAuthority.crt");
curl_setopt($ch, CURLOPT_URL, "https://www.domain-site.com/checkout/checkout.php" );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiesPath . "/cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiesPath . "/cookiefile.txt");
curl_setopt($ch, CURLOPT_COOKIE, session_name() . '=' . session_id());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$postResult = curl_exec($ch);
echo $postResult;
?>
Cookies:
www.domain-site.com FALSE / FALSE 0 SECURESESSID e3psta9vbv4kribfi18jlqxxxx
www.domain-site.com FALSE /checkout/ FALSE 13052850xx basketStartTime 1305277xxx
Im not getting expected result of html.
On Firefox using firebug, here it the http request:
GET /checkout/checkout.php?buy=YTsdfjnsdjnfjsdnkjfnkjsdnfjknsdkfnksnkfn53534545= HTTP/1.1
Host: www.domain-site.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Cookie: basketStartTime=1305276911; whoson=853-1303405947712; __utma=1.1209994793.1298996427.1305226205.1305276974.7; __utmz=1.1305276974.7.4.utmcsr=exxxxportal.com|utmccn=(referral)|utmcmd=referral|utmcct=/index.php; SECURESESSID=835qpldx8f1qikueau3s9oxxxx; __utmb=1.1.10.1305276974; __utmc=1
HTML data return as expected showing basket information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您很可能无法重复使用过去的 cookie。他们通常会识别一个会话,然后该会话超时。
您应该 (1)“浏览”到登录页面,(2) 登录,然后 (3) 进行购买 POST。并在所有三个步骤中记录和使用cookie。
如果这不起作用,请从浏览器中删除所有 cookie 并记录完整的“手动”会话,然后确保您的卷曲脚本重复该序列。
Most probably you can't just re-use cookies from the past. They typically identify a session and the session times out.
You should (1) "browse" to the login page, (2) do the login and then (3) do the buy POST. And record and use cookies in all three steps.
If that doesn't work, then erase all cookies from your browers and record a full "manual" session and then make sure your curl-script repeats that sequence.