我必须使用PHP中的CURL为每个请求发送sessionId。
当我将第一个请求与sessionId一起发送时,它工作正常。
但是对于我的第3个请求,我必须重写会话。因为在我的第3个请求中,正在创建新会话,所以我需要稍后将这些新会话发送给所有请求。
例如: 在我的第一个请求中,我沿着sessionId发送类似
Request5 :sessionId:ABCD,
在这里,我无法将sessionId发送给第4个请求,我从Request3得到响应。
这里是我的代码
<?php
$fcookies = __DIR__."/cookies.txt";
$fp = fopen($fcookies, "w");
fclose($fp);
if (! is_readable($fcookies) || ! is_writable($fcookies)) {
die(
'Cookiefile ' . $fcookies . ' is not writable or readable.'
);
exit;
}
$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 => $fcookies,
CURLOPT_COOKIEJAR => $fcookies,
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 = 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_POST => TRUE,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POSTFIELDS => $fields_string,
CURLOPT_FOLLOWLOCATION => TRUE,
//CURLOPT_COOKIESESSION => TRUE,
CURLOPT_COOKIEFILE => $fcookies,
CURLOPT_COOKIEJAR => $fcookies,
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' => 'dadmin',
'pa55word' => 'dadmin01',
'logonButton' => 'Logon',
'actionStep' => 3,
);
$fields_string = http_build_query($fields);
$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_POST => TRUE,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_POSTFIELDS => $fields_string,
CURLOPT_FOLLOWLOCATION => TRUE,
//CURLOPT_COOKIESESSION => TRUE,
CURLOPT_COOKIEFILE => $fcookies,
CURLOPT_COOKIEJAR => $fcookies,
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);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_COOKIEFILE => $fcookies,
CURLOPT_COOKIEJAR => $fcookies,
//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;
?>