如何将 $_POST 值从 ruby​​ 脚本传递到 php 脚本

发布于 2024-11-08 16:48:27 字数 710 浏览 0 评论 0原文

我需要运行 cron 作业来使用 Redmine 的 reposman.rb 添加 svn 存储库。但我们还有一个 php 脚本(带有界面),使我们能够自动创建存储库。

php 脚本接收 $_POST 值,创建存储库,设置权限,分配一些组以对新创建的存储库进行读写访问。

我的问题是:

  1. 如何执行 php 脚本,而不是 reposman.rb 中的 svnadmin create 命令?
  2. 我如何为 php 脚本提供 $_POST 参数,以便它可以使用 php 脚本正确设置我的 svn 存储库。

谢谢

编辑: 以下是这两个脚本的作用的一些示例。

Ruby 脚本:

...
// Add the repos
system "svnadmin create #{path}"
...

Php 脚本:

// 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);
...

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:

  1. how could I execute the php script, instead of the svnadmin create command in reposman.rb?
  2. 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);
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浪漫人生路 2024-11-15 16:48:27

如果可以更改脚本,它应该通过文件或标准输入获取输入数据。如果不是,您可以编写一个包装器脚本,从文件/stdin 中选取数据,用它填充 $_POST (是的,PHP 超全局变量是可写的)并通过 require 调用原始脚本代码>.

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.

傲娇萝莉攻 2024-11-15 16:48:27

向您的脚本发送 http 请求 - $_POST 不打算从命令行初始化。

Send http request to your script - $_POST is not intended to be initialized from command line.

夏天碎花小短裙 2024-11-15 16:48:27

您可以使用以下方法伪造对 PHP 脚本的 POST 请求:
(好吧,它实际上比“假”更“实现”)

exec "echo 'var=123_&data=...' | "
 " REQUEST_METHOD=POST CONTENT_TYPE=application/x-www-form-urlencoded "
 " php-cgi"

请注意,它确实需要 php-cgi 二进制文件,而不是普通的 -cli 版本。并且您需要复制所有CGI 环境变量。因此,修改现有脚本以接受 $argv 参数可能会更容易。

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.

宫墨修音 2024-11-15 16:48:27

您可以从命令行运行该脚本。
像这样的东西:


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 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
终难愈 2024-11-15 16:48:27

这里需要使用 php 命令行界面 (php_cli)。您没有 $_POST,但您可以提供传递给 php 脚本的参数。从 ruby​​ 进行系统调用来执行 php 脚本(我不知道 ruby​​,所以我只发布 php 部分)。在你的 php 脚本中,从正确的 shabang 开始:

#!/usr/bin/php
<?php

调用应该类似于:
./myscript.php -v=value1 -b=value2

检查 $_SERVER['argv'] 中传递的值

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

踏月而来 2024-11-15 16:48:27

因为@Richard Knop 的评论对我的帮助超过了任何答案,所以我将回答我自己的问题来帮助其他人。

我使用 curb 将帖子发送到 php 脚本。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文