Page 1 of 1
exec/passthru/system converts float automatically
Posted: Mon Jan 25, 2010 11:28 am
by dcinadr
I am executing an external program that returns some output that I need to read (in XML format). One of the elements returned is a float in string format (e.g. <loadsize>5639.2</loadsize>). The problem is the output that is returned using exec, system, or passthru is <loadsize>0.0</loadsize>. It seems that PHP is converting this string automatically. Is there a way to make it output what is literally being output?
With passthru I am using
Code: Select all
ob_start();
passthru("<external program here");
$xml = ob_get_contents();
ob_end_clean();
In the PHP manual I ran across ob_iconv_handler(). This seems to be exactly what I need (I think) - so that I can set the encoding to UTF-8 which is what format the XML is in. However, because of limitations on our system iconv has been disabled in our PHP build.
Is there another way to get around this problem?
Re: exec/passthru/system converts float automatically
Posted: Mon Jan 25, 2010 12:16 pm
by requinix
You've blaming the wrong thing.
PHP will get exactly what the program printed out. If the program incorrectly printed "<loadsize>0.0</loadsize>" then it's the program at fault, not PHP.
Re: exec/passthru/system converts float automatically
Posted: Mon Jan 25, 2010 12:21 pm
by dcinadr
That can't be true.
If I run the program from the command line it outputs...
<loadsize>5639.2</loadsize>
If I run the program from PHP using passthru it outputs...
<loadsize>0.0</loadsize>
Re: exec/passthru/system converts float automatically
Posted: Mon Jan 25, 2010 12:28 pm
by AbraCadaver
I assume that this program is calculating the loadsize or retrieving it from somewhere? If so, then the apache/web sever user may not have the proper environment or permissions for this program to do that.
Re: exec/passthru/system converts float automatically
Posted: Mon Jan 25, 2010 12:32 pm
by dcinadr
But I get the same results if I use the PHP command line interface. And that isn't using apache user.

Re: exec/passthru/system converts float automatically
Posted: Mon Jan 25, 2010 12:54 pm
by AbraCadaver
Then there is a different between the command you type and what you are passing to passthru(). Maybe post both?
Re: exec/passthru/system converts float automatically
Posted: Mon Jan 25, 2010 1:08 pm
by dcinadr
Here is the PHP code:
Code: Select all
ob_start();
passthru("sudo /opt/bin/loadinfo");
$rawXml = ob_get_contents();
ob_end_clean();
var_dump($rawXml);
Output:
<loadsize>0.0</loadsize>
Here is what I type in the command line:
Output:
<loadsize>5639.2</loadsize>