emurad
emurad

Reputation: 3578

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

I need this in Ruby:

$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("user:pass")
    )
));

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

Thanks.

Upvotes: 1

Views: 327

Answers (1)

Jonathan Tran
Jonathan Tran

Reputation: 15276

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
}

Upvotes: 2

Related Questions