0

I'm getting some data from an API. I want to save those data in my user session so that the API isn't called each time the page is reached.

  1. First time: load API, save in session
  2. After, load session

So this is my simple class :

class CadencierDataSession extends CustomerUserData
{

    public function getCadencier() {
        return $this->_customerSession->getCadencier();
    }

    public function setCadencier($cadencier) {
        $this->_customerSession->setCadencier($cadencier);
    }

    public function unsetCadencier() {
        $this->_customerSession->unsCadencier();
    }

}

This is how it's called :

$cadencierDataSession = new CadencierDataSession();
$cadencier = $cadencierDataSession->getCadencier();
$follow = "from session";
if($cadencier == null) {
  $follow = "from api";
  $cadencier = (new Cadencier())->get_cadencier($codeClient);
  $cadencierDataSession->setCadencier($cadencier);
  die('ok');
}

When I reach the setCadencier line, Magento throught this error :

Fatal error: Uncaught Exception: Serialization of 'SimpleXMLElement' is not allowed in /vendor/magento/framework/Session/SessionManager.php:139 Stack trace: #0 /vendor/magento/framework/Session/SessionManager.php(139): session_write_close() #1 [internal function]: Magento\Framework\Session\SessionManager->writeClose() #2 {main} thrown in /vendor/magento/framework/Session/SessionManager.php on line 139

Who to avoid it? I'd like to keep the XML structure so that I don't have to recode this page.

1 Answer 1

0

The SimpleXMLElement returned from the API is an object, and you most likely want to get a string representation. Try the following:

$cadencierDataSession = new CadencierDataSession();
$cadencier = $cadencierDataSession->getCadencier();
$follow = "from session";
if($cadencier == null) {
  $follow = "from api";
  $cadencier = (new Cadencier())->get_cadencier($codeClient);
  $cadencierDataSession->setCadencier($cadencier->asXML());
  die('ok');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.