Forum: Ruby Get file over web

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B397b498cc02503a2d86c86176f7fd3e?d=identicon&s=25 Magnus Holm (judofyr)
on 2006-06-12 21:22
Hi! I'm looking for a function to get the contents from a file on the
web into a string. Something like in PHP: $string =
file_get_contents("http://php.net");
A0c079a7c3c9b2cf0bffebd84dc578b0?d=identicon&s=25 unknown (Guest)
on 2006-06-12 21:41
(Received via mailing list)
On Jun 12, 2006, at 2:22 PM, Magnus Holm wrote:

> Hi! I'm looking for a function to get the contents from a file on the
> web into a string. Something like in PHP: $string =
> file_get_contents("http://php.net");

     require 'net/http'
     require 'uri'

     url = URI.parse('http://www.example.com/index.html')
     res = Net::HTTP.start(url.host, url.port) {|http|
       http.get('/index.html')
     }
     puts res.body

The HTTP get is put into the 'res' variable. 'res.body' is the String.

See the documentation for Net::HTTP at http://ruby-doc.org/stdlib/

cr
D72ef5027d7dbbe1563492e2de4d2d5c?d=identicon&s=25 A. S. Bradbury (Guest)
on 2006-06-12 21:48
(Received via mailing list)
On Monday 12 June 2006 20:22, Magnus Holm wrote:
> Hi! I'm looking for a function to get the contents from a file on the
> web into a string. Something like in PHP: $string =
> file_get_contents("http://php.net");

Check out open-uri:
http://ruby.outertrack.com/file/ruby-1.8.4%2Flib%2...
This is one possibility:

  uri = URI.parse("http://php.net")
  string = uri.read

Alex
This topic is locked and can not be replied to.