簡體   English   中英

curl請求后如何設置會話

[英]How to set a session after curl request

我試圖根據curl標頭的響應設置會話。 然后使用先前的會話集執行新的curl。 但是看來我仍然沒有登錄。

firl cURL執行如下所示:

$url = "https://www.tyre24.com//nl/nl/user/login/userid/MYUSERID/password/MYPASSWORD/page/L2V4cG9ydC9kb3dubG9hZC90L01nPT0vYy9NVFE9Lw==";


$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$response = curl_exec($ch); 

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

curl_close($ch);

然后從標頭響應中檢索PHPSESSID,我使用以下代碼:

$arrResult = explode("PHPSESSID=", $header);
$arrResult = explode(";", $arrResult[1]);

session_id("$arrResult[0]");
session_start();

到目前為止,一切都很好,當我執行代碼時,我在變量中獲得了PHPSESSID的值。

但是,當我將會話設置為底部有兩行時,似乎沒有任何設置。 會話開始后,我嘗試打印所有會話。 但是它會打印一個空數組。

可能是我在這里做錯了。

但是在第一個請求之后需要第二個請求,因為第一個請求的URL會將我鏈接到新的URL以下載文件。

因此,第二個請求如下所示:

$second_url = "https://www.tyre24.com/nl/nl/export/download/t/Mg==/c/MTQ=/";

$fp = fopen ('result.zip', 'w+');

$ch = curl_init(str_replace(" ","%20",$second_url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);

curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);

$response = curl_exec($ch); 

// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
$redirect_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

curl_close($ch);
fclose($fp);

誰能看到我在做什么錯? 還是可以通過一個cURL調用完成所有操作?

我不斷收到未登錄的消息。

PHPSESSID是一個cookie,因此您可以存儲該cookie以及第一個curl GET請求中的所有其他cookie,並在第二個請求中使用它們。

在第一個請求之前,為cookie信息定義一個臨時存儲文件

$cookiestore=tempnam( sys_get_temp_dir(), '_cookiejar_' );

在第一個請求中捕獲並保存Cookie

curl_setopt( $ch, CURLOPT_COOKIEFILE, $cookiestore );
curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookiestore );

在隨后的curl請求中,將cookie與請求的其他部分一起發送。

curl_setopt( $ch, CURLOPT_COOKIE, $cookiestore );

希望這會有所幫助

在整理一個可以實現cookie捕獲以及隨后的登錄和文件下載的腳本方面還花了一點時間。 由於我沒有憑據,因此未經過測試-也許會有用嗎?

<?php
    $baseurl='https://www.tyre24.com/nl/nl/user/login/page/L25sL25sL3VzZXIv';
    /* download cacert from curl.haxx.se and edit path */
    $cacert=realpath( 'c:/wwwroot/cacert.pem' );
    /* temporary cookie file */
    $cookiestore=tempnam( sys_get_temp_dir(), '_cookiejar_' );

    $zipfile='result.zip';

    /* login details */
    $params=array(
        'userid'    =>  '123abc',
        'password'  =>  'xyz999'
    );

    $headers=array();


    /* stage 1: get the page, store cookies - mmm-cookies */
    $curl=curl_init( $baseurl );
    /* set some base options used for all requests */
    $baseoptions=array(
        CURLOPT_SSL_VERIFYPEER  =>  false,
        CURLOPT_SSL_VERIFYHOST  =>  2,
        CURLOPT_CAINFO          =>  $cacert,
        CURLOPT_AUTOREFERER     =>  true,
        CURLOPT_FOLLOWLOCATION  =>  true,
        CURLOPT_FORBID_REUSE    =>  false,
        CURLOPT_FAILONERROR     =>  false,
        CURLOPT_HEADER          =>  false,
        CURLOPT_RETURNTRANSFER  =>  true,
        CURLOPT_CONNECTTIMEOUT  =>  15,
        CURLOPT_TIMEOUT         =>  90,
        CURLOPT_USERAGENT       =>  $_SERVER['HTTP_USER_AGENT'],
        CURLINFO_HEADER_OUT     =>  false,
        CURLOPT_VERBOSE         =>  true
    );

    /* specific options for initial request where you need to capture cookies */
    $options=array_merge( $baseoptions, array(
        CURLOPT_COOKIEFILE      =>  $cookiestore,
        CURLOPT_COOKIEJAR       =>  $cookiestore    
    ));

    /* set the options */
    curl_setopt_array( $curl, $options );
        $result=curl_exec( $curl );
        $info=(object)curl_getinfo( $curl );



    if( $info->http_status==200 ){
        /* stage 2: send login parameters via POST */
        $params=http_build_query( $params );

        $fp = fopen( $zipfile, 'w+');

        $headers[]='Content-Length: '.strlen( $params );

        $options=array_merge( $baseoptions, array(
            CURLOPT_FILE            =>  $fp,
            CURLOPT_COOKIE          =>  $cookiestore,
            CURLOPT_FRESH_CONNECT   =>  false,
            CURLOPT_POST            =>  true,
            CURLOPT_POSTFIELDS      =>  $params,
            CURLOPT_HTTPHEADER      =>  $headers
        ));

        curl_setopt_array( $curl, $options );
            $result=curl_exec( $curl );
            $info=(object)curl_getinfo( $curl );

            if( $info->http_status==200 ){
                /* do other stuff */
            }



        @fclose( $fp );
    } else {
        print_r( $info );
    }


    curl_close( $curl );
    $curl = $result = $info =$baseurl = $params = null;

    echo 'done';
?>

暫無
暫無

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

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