Accessing other websites
Accessing other sites with PHP is extremely easy. But why would you want to access another website? If you just want to get information from a website, for instance a website that has a weather report on a city that is of interest to you, then you can write a spider and get that information and more. I said accessing websites with PHP is easy because you access a website in pretty much the same way that you would access a text file on your hard drive, i.e. by using fopen().
The fopen() function that we use to open files is also used to open web pages, because a web page is essentially a file on a server. fopen() has the following modes:
You will only be able to open a file for reading, unless the permissions are set otherwise. Also you will have to use a trailing slash after a directory because fopen does not support redirects. For example:
the above example is fine, but the one below will fail:
Once youve opened a file, you can pretty much treat it the same way you would any other file, using common functions such as file() or fgets() to place or retrieve the data.
An example in code might be something like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<form action="tester.php" method="get">
<input name="url" type="text" />
<input name="" type="button" />
And the processing code might look something like this:
blog comments powered by Disqus