dsoihsnz85757 2015-10-26 15:05
浏览 87
已采纳

rails如何使用http身份验证将http请求发送到php文件

I have a php file test.php which created HTTP Auth.

$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>";
            }

Now I want to visit that test.php and send a get request there from a rails platform, I have tried this:

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

It doesn't work, says unauthorized. I can do changes in PHP as well as Rails, I just need a secure way to make http requests between the two.

  • 写回答

1条回答 默认 最新

  • duanbiao4035 2015-10-26 15:24
    关注

    This worked

    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
    

    thanks to MarcB

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题