繁体   English   中英

Rails如何将带有HTTP身份验证的HTTP请求发送到php文件

[英]rails how to send http request with http authentication to a php file

我有一个创建HTTP身份验证的php文件test.php。

$valid_passwords = array ("test" => "test123");
        $valid_users = array_keys($valid_passwords);

        $user = $_SERVER['PHP_AUTH_USER'];
        $pass = $_SERVER['PHP_AUTH_PW'];

        $validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

        if (!$validated) {
          header('WWW-Authenticate: Basic realm="My Realm"');
          header('HTTP/1.0 401 Unauthorized');
          die ("Not authorized");
        }

        // If arrives here, is a valid user.
        echo "<p>Welcome $user.</p>";
        echo "<p>Congratulation, you are into the system.</p>";
            }

现在,我想访问该test.php并从Rails平台发送一个get请求,我已经尝试过了:

url = 'http://url/test.php'
        uri = URI.parse(url)
        http = Net::HTTP.new(uri.host, uri.port)
        request = Net::HTTP::Get.new(uri.path,'Authorization' => "test test123")
        response = http.request(request)
        render :text => response.body

未经授权说,它不起作用。 我可以在PHP和Rails中进行更改,我只需要一种在两者之间发出http请求的安全方法。

这工作

url = 'http://url'
        uri = URI.parse(url)
        http = Net::HTTP.new(uri.host, uri.port)
        request = Net::HTTP::Get.new(uri.path, 'Content Type' => 'application/json')
        request.basic_auth("test", "test123")
        response = http.request(request)
        render :text => response.body

感谢MarcB

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM