I’m currently using the Botman framework to make my bot read a XML file.
Currently, my bot is able to grab data from an XML file and output it.
I’m having issue saving the XML file back into a global variable (so I can reuse later on in the code). Here is the current error message I get when trying to do this:
"message": "Serialization of 'SimpleXMLElement' is not allowed", "exception": "Exception", "file": "C:\Users\Jack\finalyearproject\gfyp\gfyp\vendor\opis\closure\src\SerializableClosure.php
I’m having issues here:
public function nodeTest($xmlFile, $answer) { $this->XMLFile = $xmlFile; ... }
Here is the class code before the function:
class StartConversation extends Conversation { public $XMLFile; ... public function askForDatabase() { $question = Question::create('Test test test?') ->fallback('Unable to create a new database') ->callbackId('create_database') ->addButtons([ Button::create('Suicide')->value('suic'), Button::create('Self-harm')->value('sh'), ]); $this->ask($question, function (Answer $answer) { $xmlResult = $this->testXMLGrabFunction($answer); if ($answer->getValue() == 'suic') { $this->nodeTest($xmlResult, $answer); } if ($answer->getValue() == 'sh') { $this->nodeTest($xmlResult, $answer); } }); } }
Here is the class where I get the XML file originally:
class testClass { function getXMLCategory($categoryName) { $xml = simplexml_load_file('ST-working-age-23-3-20.xml'); if($categoryName == 'suic') { $xml = $xml->node[0]; return $xml; } elseif($categoryName == 'sh') { $xml = $xml->node[1]; return $xml; } else { return null; } } }
Any suggestions would be great – thanks
Advertisement
Answer
The error message is telling you that somewhere in the code is trying to serialize the object, that is turn it into a string representation. This is probably in the framework you’re using, and what you are thinking of as a “global variable” is actually stored between requests in some form of session, e.g. in a file on disk.
Because of the way SimpleXML is implemented, it doesn’t allow this operation. The simplest workaround is to instead store the XML by calling ->asXML()
, and then re-parse it when you need it with simplexml_load_string()
.
You’ll want to do that round trip as rarely as possible, so it will be worth understanding better about how the “global variables” are actually handled by the framework so you can try to do it once on each request.