November 2002
Intermediate to advanced
640 pages
16h 33m
English
You want to retrieve the contents of a URL. For example, you want to include part of one web page in another page’s content.
Pass the URL to fopen( )
and get the contents of the page with
fread( ):
$page = '';
$fh = fopen('http://www.example.com/robots.txt','r') or die($php_errormsg);
while (! feof($fh)) {
$page .= fread($fh,1048576);
}
fclose($fh);You can use the cURL extension:
$c = curl_init('http://www.example.com/robots.txt');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$page = curl_exec($c);
curl_close($c);You can also use the
HTTP_Request
class from PEAR:
require 'HTTP/Request.php';
$r = new HTTP_Request('http://www.example.com/robots.txt');
$r->sendRequest();
$page = $r->getResponseBody();You can put a username and password in the URL if you need to
retrieve a protected page. In this example, the username is
david, and the password is
hax0r. Here’s how to do it with
fopen( )
:
$fh = fopen('http://david:hax0r@www.example.com/secrets.html','r')
or die($php_errormsg);
while (! feof($fh)) {
$page .= fread($fh,1048576);
}
fclose($fh);Here’s how to do it with cURL:
$c = curl_init('http://www.example.com/secrets.html');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_USERPWD, 'david:hax0r');
$page = curl_exec($c);
curl_close($c);Here’s how to do it with
HTTP_Request
:
$r = new HTTP_Request('http://www.example.com/secrets.html'); $r->setBasicAuth('david','hax0r'); $r->sendRequest(); $page ...Read now
Unlock full access