I´m trying to create a complete XML file with laravel and SimpleXMLElement.
I´m reading that i can to build this file from associative array and i´m trying it. I need create this xml to send this file to send it API.
But my problem it´s, when i try build my file, return this message:
Call to undefined function App\Classes\array_to_xml()
to create my file, i build a personaliced class that contain any method to build logics. I´m calling to my class, so:
$logalty = new Logalty();
$xml_user_info = new \SimpleXMLElement('<?xml version="1.0"?><user_info></user_info>');
$logalty->array_to_xml($allData, $xml_user_info);
And when i´m calling my function, return previous message.
In my class Logalty() i have my function array_to_xml() that contain:
public function array_to_xml($data, &$xml_user_info)
{
foreach($data as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_user_info->addChild("$key");
array_to_xml($value, $subnode);
}else{
$subnode = $xml_user_info->addChild("item$key");
array_to_xml($value, $subnode);
}
}else {
$xml_user_info->addChild("$key",htmlspecialchars("$value"));
}
}
//creating object of SimpleXMLElement
$xml_user_info = new \SimpleXMLElement("<?xml version=\"1.0\"?><user_info></user_info>");
//function call to convert array to xml
array_to_xml($data, $xml_user_info);
//saving generated xml file
$xml_file = $xml_user_info->asXML('users.xml');
//success and error message based on xml creation
if($xml_file){
echo 'XML file have been generated successfully.';
}else{
echo 'XML file generation error.';
}
}
I don´t know why return this message. I try instead of call to array_to_xml, call $this->array_to_xml(), but delaey so much. it´s my first time with xml in php and i don´t know very well.
Thanks for readme and sorry for my bad english
Finally, i resolve my problem with this code:
first in my controller i change my call to my class, instead of:
And in my
class logalty()This return my xml, and save in my folder. My inital problem it´s that i was doing a recursive function with no sense.