Q: How do I retrieve a page from a web site?
A: Pass a URL to file() or file_get_contents(). The former returns the
contents as an array of lines. The latter returns the same as string.
Example:
$html = file_get_contents("http://www.cnn.com");
Q: How do I retrieve a page from a web site that does browser detection?
A: Use ini_set() to change the configuration option "user_agent." This sets
the User-Agent header sent by PHP.
Example:
ini_set('user_agent', "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
$html = file_get_contents("http://www.msn.com");
Q: How do I retrieve a page from a web site that requires a cookie?
A: Use stream_context_create() to create a HTTP context with Cookie as one
of the headers. Then, if you are coding in PHP 5, pass the context to file()
or file_get_contents() as the third parameter. In PHP 4 either function
accepts a context, so you need to open the URL with fopen() and retrieve the
data a chunk at a time with fread().
Example:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>
"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
$context = stream_context_create($opts);
$f = fopen($url, "rb", false, $context);
while($data = fread($f, 1024)) {
echo $data;
}
stream_context_create() is available in PHP 4.3.0 and above. If you are
using an older version, you would need the cURL functions.
[cURL example here]