0

I want to write a function to retrieve the contents of a webpage so it works like:

$url = "example.com";
$pageContent = RetrievePageContent($url);

What would be the easiest way to do this?

Thanks in advance for your help!

MrGreggles
  • 6,113
  • 9
  • 42
  • 48

3 Answers3

6
$pageContent = file_get_contents($url);

From: file_get_contents

Michael Todd
  • 16,679
  • 4
  • 49
  • 69
4

The easiest way is file_get_contents, which does exactly what you want:

$url = "http://example.com";
$pageContent = file_get_contents($url);
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
Kyle Cronin
  • 77,653
  • 43
  • 148
  • 164
  • You must specify the http:// in the URL for PHP to know which method by which to access the resource. – Nolte Jun 07 '09 at 07:59
1

You can use the fopen command and pass it a URL. However, this ability might be disabled by your host. The best practice would be to use the cURL functions. The documentation includes sample code that does exactly what you're trying to do.

Caleb Groom
  • 155
  • 4