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

后端 未结 5 1568
野性不改
野性不改 2020-12-09 00:08

I need to turn HTML into equivalent Markdown-structured text.

OBS.: Quick and clear way of doing this with PHP & Python.

As I am programming in PHP, som

5条回答
  •  不思量自难忘°
    2020-12-09 00:56

    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($pipes[0], 'hello world');
        fclose($pipes[0]);
    
        echo stream_get_contents($pipes[1]);
        fclose($pipes[1]);
    
        // 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.

提交回复
热议问题