How to run Ruby/Python scripts from inside PHP passing and receiving parameters?

Have PHP open the Ruby or Python script via proc_open piping the HTML into STDIN in the script. The Ruby/Python script reads and processes the data and returns it via STDOUT back to the PHP script, then exits. This is a common way of doing things via popen like functionality in Perl, Ruby or Python and is nice because it gives you access to STDERR in case something blows chunks and doesn't require temp files, but it's a bit more complex Alternate ways of doing it could be writing the data from PHP to a temporary file, then using system exec or something similar to call the Ruby/Python script to open and process it, and print the output using their STDOUT EDIT: See Jonke's answer for "Best practices with STDIN in Ruby?" for examples of how simple it is to read STDIN and write to STDOUT with Ruby How do you read from stdin in python has some good samples for that language This is a simple example showing how to call a Ruby script, passing a string to it via PHP's STDIN pipe, and reading the Ruby script's STDOUT: Save this as "test.

Php":? Php $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", ". /error-output.

Txt", "a") // stderr is a file to write to ); $process = proc_open('ruby . /test. Rb', $descriptorspec, $pipes); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.

Txt fwrite($pipes0, 'hello world'); fclose($pipes0); echo stream_get_contents($pipes1); fclose($pipes1); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); echo "command returned $return_value\n"; }? Save this as "test. Rb":!

/usr/bin/env ruby puts "#{ ARGF. Read }Php hello world command returned 0 The PHP script is opening the Ruby interpreter which opens the Ruby script. PHP then sends "hello world" to it.

Ruby wraps the received text in bold tags, and outputs it, which is captured by PHP, and then output. There are no temp files, nothing passed on the command-line, you could pass a LOT of data if need-be, and it would be pretty fast. Python or Perl could easily be used instead of Ruby EDIT: If you have: HTML2Markdown.

New('HTMLcode'). To_s as sample code, then you could begin developing a Ruby solution with:! /usr/bin/env ruby require_relative 'html2markdown' puts HTML2Markdown.

New("#{ ARGF. Read }"). To_s assuming you've already downloaded the HTML2Markdown code and have it in the current directory and are running Ruby 1.9.2.

Have PHP open the Ruby or Python script via proc_open, piping the HTML into STDIN in the script. The Ruby/Python script reads and processes the data and returns it via STDOUT back to the PHP script, then exits. This is a common way of doing things via popen-like functionality in Perl, Ruby or Python and is nice because it gives you access to STDERR in case something blows chunks and doesn't require temp files, but it's a bit more complex.

Alternate ways of doing it could be writing the data from PHP to a temporary file, then using system, exec, or something similar to call the Ruby/Python script to open and process it, and print the output using their STDOUT. EDIT: See @Jonke's answer for "Best practices with STDIN in Ruby? " for examples of how simple it is to read STDIN and write to STDOUT with Ruby."How do you read from stdin in python" has some good samples for that language.

This is a simple example showing how to call a Ruby script, passing a string to it via PHP's STDIN pipe, and reading the Ruby script's STDOUT: Save this as "test. Php": array("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", ". /error-output.

Txt", "a") // stderr is a file to write to ); $process = proc_open('ruby . /test. Rb', $descriptorspec, $pipes); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.

Txt fwrite($pipes0, 'hello world'); fclose($pipes0); echo stream_get_contents($pipes1); fclose($pipes1); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); echo "command returned $return_value\n"; }? > Save this as "test. Rb": #!

/usr/bin/env ruby puts "#{ ARGF. Read }" Running the PHP script gives: Greg:Desktop greg$ php test. Php hello world command returned 0 The PHP script is opening the Ruby interpreter which opens the Ruby script.

PHP then sends "hello world" to it. Ruby wraps the received text in bold tags, and outputs it, which is captured by PHP, and then output. There are no temp files, nothing passed on the command-line, you could pass a LOT of data if need-be, and it would be pretty fast.

Python or Perl could easily be used instead of Ruby. EDIT: If you have: HTML2Markdown. New('HTMLcode').

To_s as sample code, then you could begin developing a Ruby solution with: #! /usr/bin/env ruby require_relative 'html2markdown' puts HTML2Markdown. New("#{ ARGF.

Read }"). To_s assuming you've already downloaded the HTML2Markdown code and have it in the current directory and are running Ruby 1.9.2.

Thank you very much. Would you have an example to give me please? – Roger Jan 7 at 1:35 @Roger, the PHP links for "proc_open", "system" and "exec" have example code at the bottom of the pages.

See the edit in my answer for examples for Ruby and PHP. – the Tin Man Jan 7 at 3:12 I executed your code and the output was: "command returned 1". It looks that "proc_open()" gives us much more control over the data, although, as I never used it, it seams pretty confuse at first.

I'm still picturing how to make Ruby (or Python) process the HTML input. – Roger Jan 7 at 3:57 The output says "command returned 1" because something wasn't done correctly. Try running echo 'foo' | ruby test.

Rb from the same directory where you saved the "test. Rb" file. You should get "foo".

– the Tin Man Jan 7 at 4:02 Sure, I got it: "hello world command returned 0". Now picturing how to make it happen inside Ruby or Python. – Roger Jan 7 at 4:17.

In Python, have PHP pass the var as a command line argument, get it from sys. Argv (the list of command line arguments passed to Python), and then have Python print the output, which PHP then echoes. Example: #!

Usr/bin/python import sys print " sys. Argv1 # 2nd element, since the first is the script name PHP: The procedure should be basically the same in Ruby.

Rafe, please, I have added a comment to you reply above. – Roger Jan 7 at 2:37 @Roger I see what you've done, and I'm not sure what could be producing the error. What's the output?

– Rafe Kettler Jan 7 at 3:10 @Roger I've taken a look at it and the problem is the! Character; this has special significance to the bash shell. So what you can do is escape special characters, or pick a more robust solution (I'm sure that there are markdown modules for PHP) – Rafe Kettler Jan 7 at 3:17 As I said above, if there is any better way to do this (pass the html text to other program) using PHP's "exec()", then I agree with you, it is not a solution.By the way, I am looking for a way to transform HTML into Markdown and not the opposite.

– Roger Jan 7 at 3:36 @Roger you should look into Markdownify (milianw. De/projects/markdownify). You can pass text around from PHP to Python and back, but it would be way easier to just use a PHP module.

– Rafe Kettler Jan 7 at 3:38.

Use a variable in the Ruby code, and pass it in as an argument to the Ruby script from the PHP code. Then, have the Ruby script return the processed code into stdout which PHP can read.

I think your question is wrong. Your problem is how to convert from HTML to Markdown. Am I right?

Try this milianw.de/projects/markdownify/ I think it could help you =).

As I saind, it is broken. At sourceforge. Net/projects/markdownify there is a "NOTE: unsupported - do you want to maintain this project?

Contact me! Markdownify is a HTML to Markdown converter written in PHP. See it as the successor to html2text.

Php since it has better design, better performance and less corner cases. " – Roger Jan 8 at 13:44 Evaldo, this solved my problem: stackoverflow. Com/questions/4686842/… – Roger Jan 14 at 14:28.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions