homepage Welcome to WebmasterWorld Guest from 54.237.213.31
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Dynamic XML (SimpleXML)
webfoo

5+ Year Member



 
Msg#: 4435932 posted 7:49 pm on Apr 1, 2012 (gmt 0)

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?

 

yplp



 
Msg#: 4435932 posted 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);

webfoo

5+ Year Member



 
Msg#: 4435932 posted 1:10 am on Apr 2, 2012 (gmt 0)

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?

yplp



 
Msg#: 4435932 posted 5:21 am on Apr 2, 2012 (gmt 0)

Aren't you missing include('script_a.php'); line here?

webfoo

5+ Year Member



 
Msg#: 4435932 posted 12:27 pm on Apr 2, 2012 (gmt 0)

I've tried it with and without that line, still doesn't work.

yplp



 
Msg#: 4435932 posted 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.

webfoo

5+ Year Member



 
Msg#: 4435932 posted 1:51 pm on Apr 2, 2012 (gmt 0)

Yes, it is echoing XML. You can see XML in the browser, and yes it is valid.

rocknbil

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



 
Msg#: 4435932 posted 4:31 pm on Apr 2, 2012 (gmt 0)

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!"); }

webfoo

5+ Year Member



 
Msg#: 4435932 posted 9:40 pm on Apr 5, 2012 (gmt 0)

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");

rocknbil

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



 
Msg#: 4435932 posted 12:01 am on Apr 6, 2012 (gmt 0)

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. :-\

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved