Page 1 of 1

How do you run Python/Perl scripts and return the output

Posted: Sun May 01, 2005 2:46 am
by eyevee99
Hi,

I'm writing a simple CMS for a friend and have a query regarding running perl or python scripts from within php and returning the output for use in a HTML page.

eg, the user inputs some text and submits it to the database. A script parses that text before it is saved. Then when it is a retrieved another script parses it before it is displayed.

I'm implementing Markdown via Markdown.pl and html2text via html2text.py.

Both of these scripts run and work ine on the command line. However I am having significant trouble getting them to work from within php.

I'm currently using "system()" to run them, but I can't work out how to use the output.

I want to write a function that a I pass a string to and it returns the parsed output.

eg,

Code: Select all

function html2text($string)
{
  // parse text and return output
  ob_start();
  system("echo ".escapeshellarg($string)."|blah/html2text.py");
  $buffer             = ob_get_contents();
  ob_end_clean();

  return $buffer;
}

function text2html($string)
{
  ob_start();
  system("blah/Markdown.pl ".escapeshellarg($string));
  $buffer             = ob_get_contents();
  ob_end_clean();

  return $buffer;
}
This however always returns nothing. The reteurn code form system is "0" which I believe means executed without errors. Am I approaching this correctly?

Posted: Sun May 01, 2005 2:57 am
by John Cartwright
look at this example

Code: Select all

<?php
echo '<pre>';

// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('ls', $retval);

// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>
you also never set $buffer

Posted: Sun May 01, 2005 3:01 am
by eyevee99
Yeah I left a line out of my initial post, and between our edits it got lost again while I was trying to add it.

:)

Code: Select all

$buffer             = ob_get_contents();

Posted: Sun May 01, 2005 3:12 am
by eyevee99
It was just a bracket in the wrong place. I've been coding for too long today... Thanks for your help.