skip to Main Content

I need to call an API more than once from same page and keep all the cURL requests active within their respective sessions, so that I can complete all of them at the end, once the user presses the finish button.

I can do single cURL from start to finish and then 2nd cURL from start to Finish, but not able to do both in parallel as both are fired on press of separate buttons.

can anyone suggest the way out?

I tried using cURL _Multi bu I am not able to achieve what I am looking for. the session expires for the first call as soon as the 2nd one is fired.

EDIT 1: I have to access an API to book a hotel. It only allows me to book a hotel if I keep the cUrl session same from SEARCH to BOOKING commands. This part for a single booking is easy. But If I have more than one city for which I have to book hotel, I have to have multiple cUrl commands working from SEARCH Hotel to BOOK Hotel simultaneously. MY problem is I have to invoke the Search hotel process for every city on onclick event of different button on page. Then once the user has selected the hotels for all the cities, I have to use the same cUrl sessions to finish the Bookings.
but every search has its own curl session and I am finding it difficult to maintain that. Kindly suggest.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks for the support. I was able to achieve the required functionality by using the following script as answered by @ficuscr on the similar question.

    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
    curl_setopt($ch, CURLOPT_URL,"http://www.myterminal.com/checkpwd.asp");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "UserID=username&password=passwd");
    
    ob_start();      // prevent any output
    curl_exec ($ch); // execute the curl command
    ob_end_clean();  // stop preventing output
    
    curl_close ($ch);
    unset($ch);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
    curl_setopt($ch, CURLOPT_URL,"http://www.myterminal.com/list.asp");
    
    $buf2 = curl_exec ($ch);
    
    curl_close ($ch);
    
    echo "<PRE>".htmlentities($buf2);
    ?>  
    

  2. keep all the cURL requests active within their respective sessions

    It sounds like you don’t want to keep the "request" active, but rather you want to keep the cookie session alive.

    but not able to do both in parallel as both are fired on press of separate buttons.

    you don’t want to do them in parallel anyway, you want to do the search first, then the book second, but you want to do it in different page loads with different curl handles, but have them share the same cookie session. the solution is CURLOPT_COOKIEFILE
    have your SEARCH code do something like:

    <?php
    session_start();
    $ch=curl_init();
    $cookie_file_handle = tmpfile();
    $cookie_file_path = stream_get_meta_data($cookie_file_handle)['uri'];
    curl_setopt_array($ch,array(
    CURLOPT_COOKIEFILE => $cookie_file_path,
    (...)
    ));
    curl_exec($ch);
    curl_close($ch);
    $cookies = file_get_contents($cookie_file_path);
    $_SESSION['search_cookies'] = $cookies;
    

    then have your BOOK code do something like

    <?php
    session_start();
    $cookies = $_SESSION['search_cookies'];
    $cookie_file_handle = tmpfile();
    fwrite($cookie_file_handle, $cookies);
    $cookie_file_path = stream_get_meta_data($cookie_file_handle)['uri'];
    $ch=curl_init();
    curl_setopt_array($ch,array(
    CURLOPT_COOKIEFILE=>$cookie_file_path,
    (...)
    ));
    curl_exec($ch);
    ?>
    
    • now your BOOK curl handle will inherit your SEARCH curl’s cookie session (-: even through different page loads/button-clicks.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search