473,378 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

[FAQ] How do I retrieve a page from a web site?

My contribution to the comp.lang.php FAQ:
-------------------------------------------------------------

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]
Jul 17 '05 #1
0 1698

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: R. Rajesh Jeba Anbiah | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
0
by: Janwillem Borleffs | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
3
by: R. Rajesh Jeba Anbiah | last post by:
----------------------------------------------------------------- This is the FAQ thread where the FAQ compilation project goes. * If you wish to improve the contents, please copy the whole...
5
by: David Rasmussen | last post by:
Some sites seem to be session driven in the sense that if I visit the homepage and do a few clicks I can navigate anywhere I want, but if I paste the current location into a new browser window...
25
by: Peter Michaux | last post by:
Hi, There have been a few suggestions for changing the format of the FAQ site to make it easier to maintain. VK suggested and XML procedure. Matt Kruse suggested a wiki. I think something...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.