How to pass a $_POST value to a php script from a ruby script
I need to run a cron job to add a svn repository using reposman.rb for Redmine. But we also have a php script (with an interface) that enable us to create repositories automaticly.
The php script receives a $_POST value, create the repos, sets permissions, assign some group for read and write access to the newly created repos.
My question is this:
- how could I execute the php script, instead of the
svnadmin create
command in reposman.rb? 开发者_JS百科 - How can I give a $_POST parameter to the php script, so it could setup my svn repos correctly, using the php script.
Thanks
Edit: Here are some example of what both scripts do.
Ruby script:
... // Add the repos system "svnadmin create #{path}" ...
Php script:
// Receive the name of the repos to add $dir= $_POST["dir"]; // Do all the stuff with the permissions $cmd= "sudo /usr/local/bin/createsvnrepo $dir"; system("$cmd", $retval); ...
If it's possible to change the script, it should be getting its input data through a file or standard input. If it's not, you can write a wrapper script that picks data from file/stdin, populates $_POST
with it (yes, PHP superglobals are writable) and calls the original script through require
.
Send http request to your script - $_POST is not intended to be initialized from command line.
You can fake a POST request to a PHP script with:
(well, it's actually more "implement" than "fake")
exec "echo 'var=123_&data=...' | "
" REQUEST_METHOD=POST CONTENT_TYPE=application/x-www-form-urlencoded "
" php-cgi"
Note that it really requires the php-cgi
binary, not the normal -cli version. And you need to replicate all of the of the CGI environment variables. So it might be easier to just modify the existing script to accept $argv parameters instead.
You can run the script from command line. Something like this:
params = {'foo' => 'bar'} #etc
param_str = params.collect {|k,v| "#{CGI.escape(k)}=#{CGI.escape(v)}"}.join('&')
popen("/usr/bin/php /path/to/script.php", "w+") do |pipe|
pipe.puts(param_str)
pipe.close_write
res = pipe.read
end
You need to use the php command line interface here (php_cli). You don't have $_POST, but you can provide arguments to pass to the php script. Do a system call from ruby to execute the php script (i dunno ruby so i only post the php part). In your php script start with the right shabang:
#!/usr/bin/php
<?php
The call should be something like: ./myscript.php -v=value1 -b=value2
Check the $_SERVER['argv'] for the values passed
Since, @Richard Knop's comment helped me more than any answer, I will answer my own question to help others.
I used curb to send a post to the php script.
reposman.rb
... # Added at start of file. require 'rubygems' require 'curb' ... # Replace the creation of repos from module SCM module Subversion def self.create(path) system_or_raise "svnadmin create #{path}" end end ... end # to module SCM module Subversion def self.create(path) Curl::Easy.http_post( "http://example.com/customCreaterepos.php", # Url to the php script Curl::PostField.content('reposName', path)) # Index of php $_POST in script end end ... end
精彩评论