簡體   English   中英

PHP和cURL:如何在多個請求中保持會話活動?

[英]PHP and cURL: How to keep session alive within multiple requests?

經過一段時間的嘗試,我不得不問。 它讓我發瘋。 我編寫了一個小腳本來刮取mobile.bahn.de以獲得本地火車站的出發信息。

我正在使用CURLOPT_COOKIEFILE和Cookie Jar,但是秒請求不在遠程Web服務器上的會話內。

/* Display real time information for a specific connection by scraping mobile.bahn.de */

require_once("simple_html_dom.php");

function departure_in_seconds($from, $to, $connection_number){

      // html on mobile.bahn.de is weird. So wi
      $row_number = ($connection_number+1) * 2 - 2;

      $date = date('d.m.y');
      $time = date('H:i');

      // set post fields
      $post = [
        'queryPageDisplayed' => 'yes',
        'REQ0JourneyStopsS0A'=> 1,
        'REQ0JourneyStopsS0G' => $from,
        'REQ0JourneyStopsS0ID' => '',
        'locationErrorShownfrom' => 'yes',
        'REQ0JourneyStopsZ0A' => 1,
        'REQ0JourneyStopsZ0G' => $to,
        'REQ0JourneyStopsZ0ID' => '',
        'locationErrorShownto' => 'yes',
        'REQ0JourneyDate' => $date,
        'REQ0JourneyTime' => $time,
        'existOptimizePrice' => 1,
        'REQ0HafasOptimize1' => '0:1',
        'rtMode' => 12,
        'existRTMode' => 1,
        'immediateAvail' => 'ON',
        'start' => 'Suchen'
      ];

      /* post form fields to mobile.bahn.de */
      $html = url_to_dom('https://mobile.bahn.de/bin/mobil/query.exe/dox', $post);

      /* Scrape the correct train connection from HTML */
      $connection = str_get_html($html->find('.scheduledCon',$row_number));

      /* Find departure time information in connection HTML snippet */
      $departure_time_string = $connection->find('.bold',0)->plaintext;

      /* Find delay information in connection HTML snippet */
      $delay_string =  $connection->find('.okmsg',0);
      $delay = preg_replace("/[^0-9]/","",$delay_string);
      $delay_seconds = $delay*60;

      /* Calculate the time until departure in seconds. */
      $departure_in_seconds = strtotime($departure_time_string) + $delay_seconds - strtotime('now');

      /* Find link to train connection detail information page */
      $connection_details_url = ($connection->find('a',0)->href);

      /* THIS DOES NOT WORK! WHY?? The response is not the correct HTML */
      /* Scrape this connection detail url */
      $connection_details_html = url_to_dom($connection_details_url);
      echo $connection_details_html;


      /* Find the trainline in the HTML snippet */
      $trainline = $connection_details_html->find('.motSection',0);

      /* Return all information */
      return $departure_time_string.' Delay:'.$delay.' Train line:'.$trainline;

}

function url_to_dom($href, $post = false) {
    /*store temporary cookie files */
    $cookie_jar = tempnam('/tmp','cookie');

    $curl = curl_init();

    /* if $post is set sent this posdt fields as a post request */
    if( $post ){
      curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
      curl_setopt($curl, CURLOPT_POST, true);
    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
    curl_setopt($curl, CURLOPT_URL, $href);

    $str = curl_exec($curl);
    curl_close($curl);
    // Create a DOM object
    $dom = new simple_html_dom();
    // Load HTML from a string
    $dom->load($str);

    return $dom;
}

echo departure_in_seconds('Langenfelde', 'Altona', 0).'<br>';

在github上: https : //github.com/mtoensing/time2train/blob/1.2/index.php

這種概念證明有效。 基本上。

  1. 首先,我發布表單數據以檢索結果頁面。
  2. 其次,我點擊該結果頁面上的鏈接,以獲取旅程詳細信息。

但是最后一步是行不通的。 我得到的html數據只是首頁。 我的猜測是cURL沒有會話ID。 但是我設置了所有cURL選項,例如cookiejar和cookiefile。

任何想法? 我不認為這是防止數據刮取的保護措施。 我認為限制是我在這里的編碼技能以及我缺少的有關會話和cookie的知識。 ;-)

由於會話問題,該問題不存在。 它不起作用的原因是該URL中包含實體。

html_entity_decode修復了它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM