gpt4 book ai didi

php - 如何在 PHP 中使用 cURL 将 session 值从一个请求连续传递到另一个请求?

转载 作者:可可西里 更新时间:2023-11-01 12:35:20 27 4
gpt4 key购买 nike

当发出第一个请求时,会创建一个 session ID。相同的 session ID 被携带到第二个请求。

在我的第三个请求中,为成功登录创建了一个新的 session ID,但是当我为第三个请求打印 session ID 时,响应给出了一个不同的 session ID。为什么会这样?我想将我在第 3 次响应中获得的 session ID 发送到第 4 次?

如何实现?

enter image description here

这是我的代码:

<?php
$fp = fopen("cookies.txt", "w");
fclose($fp);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_HEADER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => 'https://192.168.2.35/cgi-bin/common/login/webLogin',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_COOKIESESSION => TRUE,
CURLOPT_COOKIEFILE => "cookies.txt",
CURLOPT_COOKIEJAR => "cookies.txt",
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_AUTOREFERER => TRUE,
));
$result = curl_exec($curl);

if (!curl_exec($curl))
{
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 1</h2>";
print_r($result);
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);

// #######################################################################################

$fields = array(
'userName' => 'dadmin',
'logonButton' => 'Logon',
'actionStep' => 2,
);
$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
CURLOPT_HEADER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => 'https://192.168.2.35/cgi-bin/common/login/webLogin',
CURLOPT_POST => TRUE,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POSTFIELDS => $fields_string,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_COOKIESESSION => TRUE,
CURLOPT_AUTOREFERER => TRUE,
));
$resp = curl_exec($curl);

if (!curl_exec($curl))
{
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 2</h2>";
print_r($resp);
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);

// ################################################################################################

$fields = array(
'userName' => urlencode('dadmin') ,
'pa55word' => urlencode('dadmin01') ,
'logonButton' => urlencode('Logon') ,
'actionStep' => urlencode(3) ,
);
$fields_string = http_build_query($fields);
curl_setopt_array($curl, array(
CURLOPT_HEADER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => 'https://192.168.2.35/cgi-bin/common/login/webLogin',
CURLOPT_POST => TRUE,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POSTFIELDS => $fields_string,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_COOKIESESSION => TRUE,
CURLOPT_AUTOREFERER => TRUE,
));
$response = curl_exec($curl);

if (!curl_exec($curl))
{
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 3</h2>";
print_r($response);
echo "<br/>";
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);

// ###########Login Completed##################

curl_setopt_array($curl, array(
CURLOPT_COOKIESESSION => TRUE,
CURLOPT_HEADER => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => 'https://192.168.2.35/cgi-bin/msg/mango/admin/controller/SubscriberMgmt',
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_AUTOREFERER => TRUE,
));
$result = curl_exec($curl);

if (!curl_exec($curl))
{
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

echo "<h2>Response 4</h2>";
print_r($result);
echo "<br/>";
$cookies = curl_getinfo($curl, CURLINFO_COOKIELIST);
print_r($cookies);
exit;
?>

最佳答案

您必须在每个请求中指定 cookie 文件,而不仅仅是在前一两个请求中:

CURLOPT_COOKIEFILE => "/tmp/cookies.txt",
CURLOPT_COOKIEJAR => "/tmp/cookies.txt",

还要确保文件实际上是可写的:

$cookieFile = '/tmp/cookies.txt';
if (! is_readable($cookieFile) || ! is_writable($cookieFile)) {
throw new \Exception(
'Cookiefile ' . $cookieFile . ' is not writable or readable.'
);
}

关于php - 如何在 PHP 中使用 cURL 将 session 值从一个请求连续传递到另一个请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54553103/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com