Welcome to WebmasterWorld Guest from 54.167.252.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Dynamic XML (SimpleXML)

     
7:49 pm on Apr 1, 2012 (gmt 0)

5+ Year Member



I have "script_a.php" which generates XML output, and "script_b.php" which uses SimpleXML to display the data nicely to the user.

Script B:
$xml = simplexml_load_file("script_a.php");

This line doesn't work because it is literally trying to convert the PHP script into a SimpleXML object!

How can I tell PHP to execute Script B, BEFORE it converts it into XML?
10:04 pm on Apr 1, 2012 (gmt 0)



ob_start();
include('script_a.php');
$script_a = ob_get_contents();
ob_end_clean();
$xml = simplexml_load_string($script_a);
1:10 am on Apr 2, 2012 (gmt 0)

5+ Year Member



Thanks, its getting closer! Here is what I've got.

ob_start(); 
$script_a = ob_get_contents('script_a.php');
ob_end_clean();
$xml = simplexml_load_string($script_a);
var_dump($xml);


The var_dump is returning bool(false), so it looks like something isn't quite working... thoughts?
5:21 am on Apr 2, 2012 (gmt 0)



Aren't you missing include('script_a.php'); line here?
12:27 pm on Apr 2, 2012 (gmt 0)

5+ Year Member



I've tried it with and without that line, still doesn't work.
1:09 pm on Apr 2, 2012 (gmt 0)



Exactly how "script_a.php" outputs XML? Does it echo it, i.e. you can see XML when opening "script_a.php" in a browser? Is XML valid?

If the problem was the one you described, it would be fixed by using ob functions, so the problem is with something else, and it's hard to diagnose without seeing any real code.
1:51 pm on Apr 2, 2012 (gmt 0)

5+ Year Member



Yes, it is echoing XML. You can see XML in the browser, and yes it is valid.
4:31 pm on Apr 2, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Yes, but

The var_dump is returning bool(false)


Tells you you're not getting a valid XML string. It's likely a path issue (or something) and the output is empty, and the only way you'll know that is with prudent error trapping.

$script_a = ob_get_contents($_SERVER['DOCUMENT_ROOT] . '/thisdirectory/script_a.php') or die ("Cannot get output from script a");
ob_end_clean();

if (!empty($script_a) {
$xml = simplexml_load_string($script_a);
// Etc.
}
else { echo The script file is empty!"); }
9:40 pm on Apr 5, 2012 (gmt 0)

5+ Year Member



How did I overlook the obvious answer! I need to simply call script_a by its full HTTP path as opposed to a local path. DUH! Thanks for your help anyways :-)

$xml = simplexml_load_file("http://www.example.com/path/script_a.php");
12:01 am on Apr 6, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Well that was my first thought, but since you were using ob_get_contents (which gets the buffer,) . . . . I presumed you wanted the output of the script, not the contents of it or as a embedded functioning code. :-\
 

Featured Threads

Hot Threads This Week

Hot Threads This Month