如何使用PHP在cURL中重写会话值?

时间:2019-02-12 10:34:25

标签: php curl cookies

我必须使用PHP中的CURL为每个请求发送sessionId。

当我将第一个请求与sessionId一起发送时,它工作正常。

但是对于我的第3个请求,我必须重写会话。因为在我的第3个请求中,正在创建新会话,所以我需要稍后将这些新会话发送给所有请求。

例如: 在我的第一个请求中,我沿着sessionId发送类似

  1. Request1 :sessionId:12345,
  2. Request2 :sessionId:12345,
  3. Request3 :sessionId:12345,//在此响应中,我得到了新的sessionId:ABCD
  4. Request4 :sessionId:ABCD,
  5. 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;
?>

enter image description here

0 个答案:

没有答案