Question

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

Was it helpful?

Solution

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);

OTHER TIPS

I think you are looking for

$url_data = file_get_contents("http://example.com/examplefile.txt");

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.

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top