开发者

PHP to Ruby snippet translation needed (Basic Authorization and URL access)

开发者 https://www.devze.com 2023-03-10 01:37 出处:网络
I need this in Ruby: $context = stream_context_create(array( \'http\' => array( \'header\'=> \"Authoriz开发者_如何学Pythonation: Basic \" . base64_encode(\"user:pass\")

I need this in Ruby:

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authoriz开发者_如何学Pythonation: Basic " . base64_encode("user:pass")
    )
));

$output = file_get_contents("http://example.com/file.php", false, $context);

Thanks.


Check out Net::HTTP.

require 'net/http'

output = nil
Net::HTTP.start('example.com') {|http|
  req = Net::HTTP::Get.new('/file.php')
  req.basic_auth 'user', 'pass'
  response = http.request(req)
  output = response.body
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号