簡體   English   中英

PHP-使用cURL從重定向URL獲取cookie,但沒有響應

[英]PHP - Using cURL to get a cookie from a redirecting URL but getting no response

我有一個cURL命令,通過它我可以對網站進行身份驗證,它會給出一個cookie作為響應,然后使用該cookie,我可以對該服務進行REST API調用

這是有效的cURL命令:

curl -v -l -H "Content-Type: application/x-www-form-urlencoded" -H "Referrer:https://mywebsiteurl.com?ticket=unique_ticket_id" -d "_charset_=UTF-8&errorMessage=User+name+and+password+do+not+match&resource=%2F&username=username%40domain.com&password=XXXXXX&nextpage=welcomeCM.jsp&viewInfo=&ticket=unique_ticket_id"  -X  POST https://mywebsiteurl.com/index.jsp

在上面的命令中,票證參數包含唯一的票證ID,該ID與網站網址一起傳遞

現在,我正在嘗試使用cURL PHP實現相同的功能,這是PHP代碼:

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/index.jsp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "_charset_=UTF-8&errorMessage=User+name+and+password+do+not+match&resource=%2F&username=username%40domain.com&password=XXXXXX&nextpage=welcomeCM.jsp&viewInfo=&ticket=unique_ticket_id");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Referrer: https://mywebsiteurl.com?ticket=unique_ticket_id'
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

echo $result;

curl_close ($ch);

這里echo $result什么也沒顯示

然后我嘗試了var_dump($result); 它給出以下輸出: string(0) "" ,這意味着$response沒有返回任何內容

之后,我嘗試了curl_getinfo並添加了以下代碼:

echo "<pre>";
$info = curl_getinfo($ch);
print_r($info);
echo "</pre>";

這給了我以下數組:

Array
(
    [url] => https://mywebsiteurl.com/index.jsp
    [content_type] => text/html;charset=UTF-8
    [http_code] => 302
    [header_size] => 1306
    [request_size] => 619
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 1.540687
    [namelookup_time] => 0.028262
    [connect_time] => 0.171774
    [pretransfer_time] => 0.462507
    [size_upload] => 280
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 181
    [download_content_length] => 0
    [upload_content_length] => 280
    [starttransfer_time] => 1.54066
    [redirect_time] => 0
    [redirect_url] => https://mywebsiteurl.com/welcomeCM.jsp?username=username@domain.com&locale=en_US
    [primary_ip] => YY.YYY.YYY.YY
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => XX.XX.XXX.XX
    [local_port] => 47692
)

現在我的目標是將cookie放入$result ,但是它是空的,什么也沒有返回

是否在cURL命令等效的PHP代碼中缺少某些內容?

有人可以幫我嗎,並指出我要檢索Cookie的方向

非常感謝!

UPDATE

我添加了curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 成功登錄后使其遵循302重定向的請求

但是這一次執行cURL之后, echo $result將我當前的本地網頁重定向到/Dashboard?viewInfo= ,這顯然不存在

也是這次var_dump($result); 結果為: string(1462) " "

curl_getinfo($ch)提供了以下數組:

Array
(
    [url] => https://mywebsiteurl.com/welcomeCM.jsp?username=username@domain.com&locale=en_US
    [content_type] => text/html;charset=UTF-8
    [http_code] => 200
    [header_size] => 1821
    [request_size] => 956
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 1
    [total_time] => 1.746911
    [namelookup_time] => 1.5E-5
    [connect_time] => 1.5E-5
    [pretransfer_time] => 6.2E-5
    [size_upload] => 0
    [size_download] => 1462
    [speed_download] => 836
    [speed_upload] => 0
    [download_content_length] => 1462
    [upload_content_length] => 0
    [starttransfer_time] => 0.146587
    [redirect_time] => 1.6003
    [redirect_url] => 
    [primary_ip] => YY.YYY.YYY.YY
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => YY.YY.YYY.YY
    [local_port] => 47705
)

仍然無法在此處獲取所需的Cookie,請幫助!

如果要使用給定的卷發功能,則可以嘗試使用CURLOPT_COOKIEJAR / CURLOPT_COOKIEFILE來存儲和獲取Cookie。 這是基於文件的。 在這種情況下,curl將在文件中寫入與Cookie相關的所有信息。

您可以定義此文件的位置。

通過使用CURLOPT_COOKIEJAR,您將告訴curl它將捕獲cookie數據。

// create cookie file
$cookie = __DIR__ . "/where/you/want/to/store/your/cookie/mycookie.txt";
fopen($cookie, "w");
// you may want to use some random identicator in your cookie file
// to make sure it does not get overwritten by some action

// prepare login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/index.jsp");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// tell curl to follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURlOPT_POSTFIELDS, "username=usernam&password=XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
// set a jar, to store your cookie
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
$headers = array(
        'Content-Type: application/x-www-form-urlencoded',
        'Referrer: https://mywebsiteurl.com?ticket=unique_ticket_id'
        );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// do login
$loginResult = curl_exec($ch);

// check if your login has worked according to the given result by any method you want
if (preg_match("/Welcome/", $loginResult)) {
    // simple regex for demonstration purpose
}

// now you could load your cookie out of the file, or just use the file for future requests

如果登錄成功,則所有信息都將存儲在cookie文件中。

現在,您可以將cookie再次與curlopt CURLOPT_COOKIEFILE一起使用,以備將來使用。

// do other request, with your cookie
curl_setopt($ch, CURLOPT_URL, "https://mywebsiteurl.com/api/rest.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "your new post data");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// set a jar to update your cookie if needed
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
// give curl the location of your cookie file, so it can send it
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

暫無
暫無

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

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