简体   繁体   中英

Failed to start the session because headers have already been sent by curl_exec()

I have seen some similar SO questions, but they did not solve my issue.

When I perform a cUrl GET, I am getting an error message:

Failed to start the session because headers have already been sent by
".../cUrlWrapper.php" at line 60.

The corresponding PHP code is:

$this->curl = curl_init($url);
curl_exec($this->curl);  // Line 60

Does anyone know what is happening and how I could solve this?

Solution Update

Although the approved solution solved my issue (workaround), I also removed an unnecessary ContainerAwareTrait in a service (root cause of my issue) which is even better in the first place.

Unless you set the "return transfer" option, curl_exec() will output the result of the URL request. Once that output has happened, you cannot set any new headers, like starting a session.

You can either call session_start() before you include the cUrlWrapper.php file (or call whatever function that curl_exec() is in), or you can catch the result of the URL request and output it later:

$this->curl = curl_init($url);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($this->curl);
// perhaps "return $result;" if this is in a function

// ...
session_start();
// ...

echo $result;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM