Page 1 of 1

Better way to load evaluated PHP XML file?

Posted: Fri Oct 19, 2012 7:31 pm
by TildeHash
Is there a better way to do this?

Code: Select all

<?php
	$feed = simplexml_load_file('http://127.0.0.1/testxml.php');
	echo $feed->test;
?>
I'm looping back in order to load a PHP file that generates XML.

Example: "testxml.php" file contains something like:

Code: Select all

<?php
	header('Content-Type: application/xml');
	echo '<?xml version="1.0" encoding="UTF-8" ?>';
?>

<content>
	<test><?php for ($i = 0; $i < 10; $i++) echo $i . ''; ?></test>
</content>
The 127.0.0.1 is adding unnecessary load time, if I'm not mistaken.

Re: Better way to load evaluated PHP XML file?

Posted: Fri Oct 19, 2012 7:50 pm
by requinix
You could

Code: Select all

ob_start();
include "testxml.php";
$feed = new SimpleXMLElement(ob_get_clean()); // or simplexml_load_string()
What would be even better is if you moved the <test> generation stuff into a function so you could use it in both places without caring about whether the content it returns goes into XML or gets processed by more code.

Re: Better way to load evaluated PHP XML file?

Posted: Fri Oct 19, 2012 11:58 pm
by TildeHash
requinix wrote:You could

Code: Select all

ob_start();
include "testxml.php";
$feed = new SimpleXMLElement(ob_get_clean()); // or simplexml_load_string()
What would be even better is if you moved the <test> generation stuff into a function so you could use it in both places without caring about whether the content it returns goes into XML or gets processed by more code.
That works! :drunk: Many thanks :D