Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

load a remote html page into a variable

Status
Not open for further replies.

solex

Programmer
Feb 28, 2003
91
GB
Hello,

I know this had prob been asked before, but ive had no luck searching, so here goes.

I want to send a http request to a web server, say and load it into a varable, how can i do this?

Thanx in advance

Charles
 
just to clarify, i want the source code of the returned file in the variable.
 
to get the source code of the file you will need to use
fopen and fread (or readfile etc) and make sure that your php installation allows you to open url's as if they were files (check your php.ini) (and the necessary permissions are ok on the remote server).

otherwise you can use the cURL functions to return the page but this will not give you the source code but the output of the page. Of course with html this is likely to be similar (barring SSI).

another alternative is to use the php ftp functions to retrieve the file.
 
yes, im looking for just the oit put that normally goes to a browser

charles
 
this would be the simplest approach
it relies on allow_url_fopen being set in your php.ini file

if you do not have control over this ini setting then see fopen in the php.net manual. there is a class posted in the user notes that performs an http get request for you. i also have a dim memory of a function already being incorporated into php that does this really easily but don't remember which one it is.

Code:
$url = "[URL unfurl="true"]http://www.domain.com/file.html";[/URL]
$url = urlencode($url);
$fh = fopen($url, "rb") or die("cannot open remote file");
//assuming you are using > php 5.0
$contents = stream_get_contents($fh);
fclose($fh);
//if you are using PHP < 5.0
/*
$contents = "";
while (!feof($fh)):
  $contents .= fread($fh, 1024);
endwhile;
fclose($fh);
*/

it is also possible that this shorter code will work
Code:
$url = "[URL unfurl="true"]http://www.domain.com/file.html";[/URL]
$url = urlencode($url);
$contents = file_get_contents($url);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top