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

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

报告相同问题?

悬赏问题

  • ¥15 为什么eprime输出的数据会有缺失?
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题