如何在不存储cookie的情况下维护cURL HTTP会话?

时间:2016-10-12 12:54:34

标签: php curl cookies

这是我用来维护带cookie的HTTP会话的PHP cURL代码

        if(!function_exists("\curl_init")){
        \load_curl();
        echo "curl loaded";
    } else {
        echo "curl already exists   ";
    }
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, '');  //could be empty, but cause problems on some hosts
    curl_setopt($ch, CURLOPT_COOKIEFILE, '');  //could be empty, but cause problems

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url1);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $startInterview);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $startInterviewHeaders);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    //curl_setopt($ch, CURLOPT_HEADER, 1);

    $startInterviewresponse = curl_exec($ch);
    //echo $response;

    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $curl_errno= curl_errno($ch);
    echo "</br> HTTP status: " . $http_status . "</br> cURL error: " .$curl_errno . "</br>";
    //curl_close($ch); //  close cURL
    echo $startInterviewresponse;

    $investigateHeaders = array("SOAPAction: xxx");

    curl_setopt($ch, CURLOPT_POSTFIELDS, $investigateXML);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $investigateHeaders);
    $investigateResponse = curl_exec($ch);
    echo "</br>";
    echo $investigateResponse;

    curl_close($ch);

问题是,应该部署此代码的系统不允许存储cookie,代码根本不起作用。 那么,如何修改此代码以使用PHP变量而不是临时文件来维护HTTP会话?

谢谢

1 个答案:

答案 0 :(得分:0)

如果您无法使用文件系统,则可以使用php://memory流将会话信息保存在内存而不是文件系统中。

或者,您可以在第一次请求时解析响应Set-Cookie标头中的会话ID,将其保存到变量并通过Cookie标头或通过CURLOPT_COOKIE发送每个变量后续请求。