PHP Networking
(Page 1 of 4 )
PHP has a great many tools for interacting with a network and also with the Internet. In this article we will look at some of those tools and functions to see how we can use them to make our scripts more useful in a network environment. This article is the first of two parts.
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().
fopen(http://localhost/websecure/ftp.php, “r”);
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:
Mode | Meaning |
r | Reading only, begins reading at the start of the file. |
r+ | Reading or writing, begins reading at the start of the file. |
w | Writing only. Creates the file if it does not exist, and overwrites any existing content. |
w+ | Reading or writing. Creates the file if it does not exist, and overwrites any existing content (when writing). |
a | Writing only. Creates the file if it does not exist, and appends the new data to the end of the file. |
a+ | Reading or writing. Creates the file if it does not exist, and overwrites any existing contents (when writing). |
x | Writing only. Creates the file if it does not exist, but does nothing, issues a warning, if the file does exist. |
x+ | Reading or Writing. Creates the file if it does not exist, but do nothing, issues a warning, if the file does exist. |
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:
fopen(“http://localhost/websecure/ftp.php/”, “r”);
the above example is fine, but the one below will fail:
fopen(http://localhost/websecure/ftp.php, “r”);
Once you’ve 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">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form action="tester.php" method="get">
<input name="url" type="text" />
<input name="" type="button" />
</form>
</body>
</html>
And the processing code might look something like this:
<?
$url=$_POST['url'];
$fp=file($url);
$n=count($fp);
$r=rand(0,($n-1));
echo trim($fp[$r]);
echo $n;
?>
Next: DNS and PHP >>
More PHP Articles
More By David Web