如何在curl_init()之后打开$ _session

时间:2014-08-03 10:41:53

标签: php session

我使用curl_init()进行xml请求和响应。但是在url_close($ch)我收到错误后将值传递给会话

session_start();
$_SESSION['mydata'] = "1";
session_write_close();

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_ENCODING,'gzip');
    curl_setopt($ch, CURLOPT_POST, true ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_request); 
    $data_response = curl_exec($ch); 
    curl_close($ch); 

    $xd = simplexml_load_string($data_response);
    if (($xd->DataCount) > 0)
    {
        session_start();
        $_SESSION['mydata'] = "0";
        session_write_close();

    }

但我收到错误

Warning: session_start() [function.session-start]: Node no longer exists in
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at

1 个答案:

答案 0 :(得分:0)

您不必关闭会话,在脚本结束时将其保持打开或关闭。您只能在发送标头之前打开会话(因为cookie)。

我建议您在此处阅读会话的基本用法:http://php.net/manual/en/session.examples.basic.php

session_start();
$_SESSION['mydata'] = "1";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_POST, true ); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_request); 
$data_response = curl_exec($ch); 
curl_close($ch); 

$xd = simplexml_load_string($data_response);
if (($xd->DataCount) > 0)
{
    $_SESSION['mydata'] = "0";
}