# Get file over web

**URL:** <https://www.ruby-forum.com/t/get-file-over-web/63163>\
**Category:** Ruby\
**Created:** [June 12, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/get-file-over-web/63163 "2006-06-12T21:22:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![judofyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90ced4/32.png) [@judofyr](https://www.ruby-forum.com/u/judofyr)\
**Post date:** [June 12, 2006, 9:22pm UTC](https://www.ruby-forum.com/t/get-file-over-web/63163/1 "2006-06-12T21:22:13Z")

</div>

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](http://php.net)”);

---

<div class="post-metadata">

**Author:** ![judofyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90ced4/32.png) [@judofyr](https://www.ruby-forum.com/u/judofyr)\
**Post date:** [June 12, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/get-file-over-web/63163/2 "2006-06-12T21:41:48Z")

</div>

On Jun 12, 2006, at 2:22 PM, Magnus H. 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](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 [RDoc Documentation](http://ruby-doc.org/stdlib/)

cr

---

<div class="post-metadata">

**Author:** ![judofyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/90ced4/32.png) [@judofyr](https://www.ruby-forum.com/u/judofyr)\
**Post date:** [June 12, 2006, 9:48pm UTC](https://www.ruby-forum.com/t/get-file-over-web/63163/3 "2006-06-12T21:48:28Z")

</div>

On Monday 12 June 2006 20:22, Magnus H. 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](http://php.net)”);

Check out open-uri:  
[http://ruby.outertrack.com/file/ruby-1.8.4%2Flib%2Fopen-uri.rb](http://ruby.outertrack.com/file/ruby-1.8.4%2Flib%2Fopen-uri.rb)  
This is one possibility:

uri = URI.parse(“[http://php.net](http://php.net)”)  
string = uri.read

Alex
