How to load arbitrary data from PHP code?

The question is simple. What function would I use in a PHP script to load data from a url to a string?

0


source to share


4 answers


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

      

+5


source


I think what you are looking for



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

      

+5


source


With file wrappers, you can use file_get_contents to access http resources (mostly GET requests only, no POST). For more complex HTTP requests, you can use curl wrappers if installed. Check php.net for more information.

+2


source


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

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

      

+1


source







All Articles