i have been created an xml file and named it 'input.xml'. When i'm trying to add a file (attach to string) in php, i get 500 browser error.
This is how i importing my xml to variable (working way):
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>Movie 1</title>
</movie>
<movie>
<title>Movie 2</title>
</movie>
<categories>
<category>
<name>Example category</name>
</category>
</categories>
</movies>
XML;
This is how i want to import my xml (from input.xml file) (not working way):
$xmlstr = simplexml_load_file("input.xml");
This is how my php code looks like:
$movies = new SimpleXMLElement($xmlstr);
foreach ($movies->movie as $mov) {
echo "We found a movie!<br>";
}
foreach ($movies->categories as $cat) {
echo "We found a category!<br>";
}
And this is my input.xml file content:
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>Movie 1</title>
</movie>
<movie>
<title>Movie 2</title>
</movie>
<categories>
<category>
<name>Example category</name>
</category>
</categories>
</movies>
$movies = simplexml_load_file("input.xml");assimplexml_load_filereturns anSimpleXMLElementObjectBackend fatal error: PHP Fatal error: Uncaught Exception: String could not be parsed as XML in ..../zil/index.php:21\nStack trace:\n#0 ....}/zil/index.php(21): SimpleXMLElement->__construct('')\n#1 {main}\n thrown in ..../zil/index.php on line 21\nMy 21 line:$movies = new SimpleXMLElement($xmlstr);trueas the third argument (data_is_url) in the constructor fornew SimpleXMLElement(), it will use the first argument as XML-data, not as a path. So you must do:new SimpleXMLElement('input.xml', 0, true). You should always refer to the manual if you can't get a PHP function/object working as expected.