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