设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 168|回复: 0

PHP page ready with cookies send back

[复制链接]
发表于 2013-11-4 15:34:48 | 显示全部楼层 |阅读模式
I want to get the html sourcecode from that page:     http://bahn.ltur.com/index/search/?lang=de_DE&searchin=DE-SB-VI&trip_mode=trip_simple&from_spar=Hamburg+Dammtor&to_spar=Leipzig+Hbf&start_datum=01.07.2012&start_time=23%3A49&end_datum=01.07.2012&end_time=23%3A51&SEA_adults=1&trainclass_spar=2
I can access the site when i go to that url. i cannot get the sourcecode via curl. not in bash "curl -iL url > site.html" nor with that php script:i get the startpage (bahn.ltur.com).

Code for sending GET request
  1. $ch=curl_init();

  2. curl_setopt($ch,CURLOPT_URL,'http://bahn.ltur.com/index/search/?lang=de_DE&searchin=DE-SB-VI&trip_mode=trip_simple&from_spar=Hamburg+Dammtor&to_spar=Leipzig+Hbf&start_datum=01.07.2012&start_time=23%3A49&end_datum=01.07.2012&end_time=23%3A51&SEA_adults=1&trainclass_spar=2');
  3. curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
  4. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  5. curl_exec($ch);
复制代码

//Answer:
First: get cookie from page (w/GET-params)Second: go to the details page and send the cookie.
  1. /* get cookie */
  2. $ckfile = tempnam ("/tmp", "CURLCOOKIE");
  3. $ch=curl_init();
  4. curl_setopt($ch,CURLOPT_URL,'http://bahn.ltur.com/index/search/?lang=de_DE&searchin=DE-SB-VI&trip_mode=trip_simple&from_spar=Hamburg+Dammtor&to_spar=Leipzig+Hbf&start_datum=01.07.2012&start_time=23%3A49&end_datum=01.07.2012&end_time=23%3A51&SEA_adults=1&trainclass_spar=2');
  5. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  6. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  8. curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
  9. $html = curl_exec($ch);
  10. /* get page */
  11. $ch = curl_init("http://bahn.ltur.com/details");
  12. curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

  14. echo $html;
  15. curl_close($ch);
复制代码

you should check the return value from curl_exec as well:
  1. if ($html === false) {
  2.    die("Curl error: " . curl_error($ch));
  3. }
复制代码

The point is:
  1. curl_setopt($ch, CURLOPT_COOKIE, "BAHNSESSID= fd12a6c3d517c702eab0014879d551ef; bla=foo");
复制代码





您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-12-12 22:31 , Processed in 0.013118 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表