views:

609

answers:

4

This question is simple. What function would I use in a PHP script to load data from a URL into a string?

+2  A: 

With file wrappers you can use file_get_contents to access http resources (pretty much just GET requests, no POST). For more complicated http requests you can use the curl wrappers if you have them installed. Check php.net for more info.

ehassler
+2  A: 

I think you are looking for

$url_data = file_get_contents("http://example.com/examplefile.txt");
chews
OK, it works. But not for wikipedia.org.For example:` file_get_contents("http://en.wikipedia.org/wiki/apple");`failsThis is a problem because this is the site that I am trying to load data from"
GameFreak
OK, NOW it works. Even though I didn't change anything.
GameFreak
+2  A: 

CURL is usually a good solution: http://www.php.net/curl


// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// grab URL and pass it to the browser
$html = curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
Keith Palmer
A: 

Check out Snoopy, a PHP class that simulates a web browser:

include "Snoopy.class.php";
$snoopy = new Snoopy;
$snoopy->fetchtext("http://www.example.com");
$html = $snoopy->results;
Derek Kurth