1

tried these commands

exec("ruby helloword.rb");
system ("ruby helloword.rb");

Running php on windows server 2012R2 I just want the ruby class to run as it will read and write results from text file and than i can user those text files. Is there any simple way to get this done. Tried almost everything on stackoverflow. So please dont mark this as duplicate.

Akif Hazarvi
  • 47
  • 1
  • 10

3 Answers3

1

I am not sure if this is what you looking for but see below :)

Se example below:

<?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 it as Save this as "test.php":

source:

Run Ruby/Python from PHP Code

Here is another good example:

//PHP script to execute ruby scripts when the host doesn't have a cgi handler for .rb
//Use with this .htaccess:

/*
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.rb$ handler.php?rb=$1.rb [NC,QSA]
*/

$file = $_GET['rb'];

if(in_array($file, scandir('.')))
{
foreach($_REQUEST as $key=>$value) if($key != 'rb') $args .= " $key=".urlencode($value);
echo exec(escapeshellcmd('./'.$file.$args));
}
else
{
echo '404- Page not found';
}
?>

Regards

Daniel

Community
  • 1
  • 1
XsiSec
  • 944
  • 8
  • 19
  • First one returned the error "ruby is not recognized as an internal or external command ", operable program or batch file – Akif Hazarvi Aug 31 '16 at 08:12
  • Have you installed ruby? also take look on http://stackoverflow.com/questions/4619996/how-to-run-ruby-python-scripts-from-inside-php-passing-and-receiving-parameters?noredirect=1&lq=1 – XsiSec Aug 31 '16 at 08:13
  • Ruby is installed and i can run it through command line. When i try to run it through php code it doesn't work. – Akif Hazarvi Aug 31 '16 at 08:14
  • Yup I don't have server/phpadmin/ruby installation, can you guide me how to install it? – Akif Hazarvi Aug 31 '16 at 08:16
  • @AkifHazarvi : Is the directory with the Ruby binary present in the PATH at the time when you execute the command? Output the PATH just before running Ruby. – user1934428 Aug 31 '16 at 09:19
  • Nop, How can i find the ruby binay path? – Akif Hazarvi Aug 31 '16 at 09:58
0

It worked by adding exec("filename"); Before it was written something like this exec('ruby filename'); Thank you very much everyone for their responses.

Akif Hazarvi
  • 47
  • 1
  • 10
-1

you can use load method of Kernel. see the documentation at http://ruby-doc.org/core-2.2.1/Kernel.html#method-i-load

Kushal
  • 322
  • 1
  • 7
  • Can you explain a bit how can i use this method if you can give an example or something. – Akif Hazarvi Aug 31 '16 at 08:02
  • you can have a look [here](https://www.practicingruby.com/articles/ways-to-load-code) – Kushal Aug 31 '16 at 08:05
  • @KushalMistry Can you please add the important part of code from the link you shared in your answer , coz over the time links go dead and if someone in future wants to refer then it tends to get difficult . – Caffeine Coder Aug 31 '16 at 08:10