exec/passthru/system converts float automatically

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

exec/passthru/system converts float automatically

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: exec/passthru/system converts float automatically

Post 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.
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

Re: exec/passthru/system converts float automatically

Post 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>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: exec/passthru/system converts float automatically

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

Re: exec/passthru/system converts float automatically

Post by dcinadr »

But I get the same results if I use the PHP command line interface. And that isn't using apache user. :?:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: exec/passthru/system converts float automatically

Post by AbraCadaver »

Then there is a different between the command you type and what you are passing to passthru(). Maybe post both?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dcinadr
Forum Newbie
Posts: 12
Joined: Fri Jan 08, 2010 4:15 pm

Re: exec/passthru/system converts float automatically

Post 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:

Code: Select all

 
sudo /opt/bin/loadinfo
 
Output:
<loadsize>5639.2</loadsize>
Post Reply