我正在尝试运行一个擦除某些URL的Ruby脚本,并从PHP执行它。
这是我的PHP代码:
PHP:
$url = 'https://www.example.com';
$result= shell_exec("ruby script.rb $url");
echo 'result is:';
echo $result;
我有两个不同的代码,一个基于Curl,它可以工作:
使用Ruby代码:
value = `curl #{ARGV[0]} | grep "findMe:"`
result = value.scan(/findMe: (.*)/).flatten.first.split('$').last.gsub(',', '').to_f
puts result
但是另一个是使用mechanize gem,它不适用于PHP:
无工作代码:
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
但是,从SSH,使用Putty,两个Ruby脚本都很完美。
那么他们中的一个怎么可能在PHP执行失败呢?
更新
据我所知,我将shell更改为:
$result = `ruby script.rb "$url" 2>&1`;
这是错误:
结果是:script.rb:2:在`require'中:没有这样的文件要加载 - 机械化 (LoadError)来自script.rb:2
也许它与路径有关,但我不确定如何正确更改它们