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

后端 未结 5 1556
野性不改
野性不改 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:43

    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 "Hello ", sys.argv[1] # 2nd element, since the first is the script name
    

    PHP:

    <?php
    echo exec('python script.py Rafe');
    ?>
    

    The procedure should be basically the same in Ruby.

    0 讨论(0)
  • 2020-12-09 00:45

    Another very weird approach will be like the one i used.

    Php file -> output.txt
    ruby file -> read from output.txt
    Ruby file-> result.txt
    Php file -> read from result.txt
    
    simple add exec(rubyfile.rb);
    

    Not recommended but this will work for sure.

    0 讨论(0)
  • 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":

    <?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($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 "<b>#{ ARGF.read }</b>"
    

    Running the PHP script gives:

    Greg:Desktop greg$ php test.php 
    <b>hello world</b>
    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('<h1>HTMLcode</h1>').to_s
    

    as sample code, then you could begin developing a Ruby solution with:

    #!/usr/bin/env ruby
    
    require_relative 'html2markdown'
    
    puts HTML2Markdown.new("<h1>#{ ARGF.read }</h1>").to_s
    

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

    0 讨论(0)
  • 2020-12-09 01:00

    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.

    0 讨论(0)
  • 2020-12-09 01:04

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

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

    0 讨论(0)
提交回复
热议问题