0

I am trying to run a Ruby script that scrapes some URL, and execute it from PHP.

This is my PHP code:

PHP:

$url = 'https://www.example.com';

$result= shell_exec("ruby script.rb $url");

echo 'result is:';

echo $result;

I have two different codes, one is based on Curl, and it works:

Working Ruby Code:

value = `curl #{ARGV[0]} | grep "findMe:"`
result = value.scan(/findMe: (.*)/).flatten.first.split('$').last.gsub(',', '').to_f
puts result

But the other one is using mechanize gem, and it doesn't work from PHP:

None Working Code:

require 'time'
require 'mechanize'

url = ARGV.first

begin
  page = Mechanize.new.get(url).content
  result = page.match(/findMe: "\$(.+)"/)[1].tr(',', '')

  # Uncomment following line to see date and time in output
  # output = Time.now.strftime('[%F %T %z] ') + result

  # Uncomment following line to see only result in output
  output = result

  # Output the result on terminal
  puts result

rescue => detail
  puts detail.message
end

But, from SSH, using Putty, both Ruby scripts work perfect.

So how could it be that one of them is failing on PHP execution?

Update:

As I was advised, I changed shell to:

$result = `ruby script.rb "$url" 2>&1`;

This is the error:

result is:script.rb:2:in `require': no such file to load -- mechanize (LoadError) from script.rb:2

Maybe it does have to do with paths, but I'm not sure how to change them correctly

rockyraw
  • 1,125
  • 2
  • 15
  • 36
  • Have you tried using the full path for both `ruby` and `script.rb` ? – Pedro Lobito Apr 30 '17 at 15:31
  • what do you mean by `ruby`? and how do I use full paths? – rockyraw Apr 30 '17 at 15:37
  • use `which ruby` on your shell to find ruby's binary full path, that should give you something like `/usr/bin/ruby`. – Pedro Lobito Apr 30 '17 at 15:39
  • This is the result: `/usr/local/rvm/rubies/ruby-2.3.3/bin/ruby` - what do I do with it? and still, why first ruby script works fine? I don't understand the logic. – rockyraw Apr 30 '17 at 15:41
  • Try using `$result= shell_exec("/usr/local/rvm/rubies/ruby-2.3.3/bin/ruby /full/path/to/script.rb '$url' ");` – Pedro Lobito Apr 30 '17 at 16:28
  • didn't help. I guess fullpath is the path I get when I do `echo $_SERVER["DOCUMENT_ROOT"`, tried it, still no result. – rockyraw Apr 30 '17 at 17:12
  • I'm guessing this has got something to do with a conflict between two ruby installations - probably the default system ruby, and `rvm`. When you execute the command over SSH, the environment is being loaded differently. Figure out what behaviour you *want* to have, and then test to see what's actually happening. – Tom Lord Apr 30 '17 at 19:26
  • It would also greatly help if you included full information in your post, such as the actual error messages being seen. Vague comments like "doesn't help" and "didn't work" makes it hard to suggest what to try. – Tom Lord Apr 30 '17 at 19:27
  • I use `error_reporting(E_ALL); ` and `ini_set('display_errors', 'On');` , no error is being reported. I don't know where can I look for the errors... – rockyraw Apr 30 '17 at 19:51
  • change that shell_exec line to `$result = \`ruby script.rb "$url" 2>&1\`;` - post the result if you need more help. – pguardiario May 01 '17 at 00:58
  • You need to `gem install mechanize` with the system default ruby or use the full path to the ruby you want. If it were me I would uninstall all rubies and rvm and install a ruby without rvm. – pguardiario May 01 '17 at 23:02

0 Answers0