发出第一个请求时,将创建一个 session ID。相同的 session ID被传送到第二个请求。
在我的第三个请求中,为成功登录创建了新的 session ID,但是当我为第三个请求打印 session ID时,响应给出的是不同的 session ID。为什么会这样呢?我想将在第3个响应中获得的 session ID发送到第4个?
如何实现呢?
这是我的代码:
<?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.'
);
}