PHP is a powerful scripting language that can do much more than simply format text on a Web page. The strength of PHP lies in its versatility. For example, PHP scripts can communicate with other servers via various protocols, such as HTTP, FTP, and SMTP, using only PHP’s internal functions. Let's look at the functions that allow PHP scripts to communicate using these three protocols.
PHP and SMTP
The first, and easiest, communication is the transfer of mail from a PHP application via SMTP. This functionality can be used in feedback forms, Web-based e-mail clients, and other common interfaces. The ability to send mail in PHP depends on one simple function called mail(), with the syntax:
mail(recipient, subject, body, headers)
The mail() function returns a value of TRUE if the message is successfully sent and FALSE if there are errors. Listing A shows an example of using the mail() function in a feedback form where the variable $text is the user input .
As you can see in Listing A, the mail() function is very straightforward. All you have to do is supply the recipient, the content for the message body, the subject for the message, and additional headers.
Since mail() has no field to specify whom the message is from, you need to use the additional headers to do so. You could ask the user for his or her specific e-mail address and set the From: portion of the extra headers to that e-mail address, instead of a dummy robot address as shown here. You could specify as many additional headers as you like by separating them with the newline character \n.
PHP and HTTP
Using PHP's HTTP functions are not quite as easy as sending e-mail, but they are simple enough. Retrieving Web pages in your code via HTTP is as simple as opening a file to read. To do this, you can use the fopen() function or the readfile() function, depending on your needs. The readfile() function is extremely straightforward, with the following syntax:
$url = “http://www.somesite.com/index.html”;
$content = readfile($url);
The code snippet above retrieves the page's index.html from www.somesite.com and stores it in the variable $content, which you can now manipulate as needed.
Listing B offers another alternative based on fopen(). You implement the local functions that you would use to call files on a remote server. Both our readfile() and fopen() examples read the contents of index.html into the variable $content. The fopen() approach is more flexible because it gives you more complete control over the data you are receiving; you can limit it to a certain number of bytes, easily create arrays from the data, and so forth.
There are a few caveats to keep in mind when retrieving remote HTTP data:
- · You can't use functions such as stat() or filesize() on URLs because they use local system calls, but almost every other function used to read local files can be used to read remote ones.
- · You can access protected information, such as data protected with a username and password. To do this, write your URL with the syntax:
While reading via HTTP is a snap, writing to remote Web sites can be tough in PHP, unless you build PHP with cURL support by passing the configure command –with-curl option when compiling PHP initially. To employ cURL, you can use the Net_Curl module, which comes from the PEAR interface. The Net_Curl module will let you get even more functionality when talking to remote Web servers by connecting via SSL, Web proxies, and other exchanges.
PHP and FTP
Support for FTP in PHP is quite extensive, with a number of functions that allow you to work with remote FTP sites. Listing C shows code to connect to a remote FTP site and download a file.
As you can see in Listing C, this is no small amount of code, which is perhaps the clearest illustration of how much more involved dealing with FTP data is than with HTTP data. The code isn’t difficult, but takes a little more time to implement. To enact the transfer, our code must take the following steps:
- 1. Download ftp.somesite.com/pub/somedir/somefile-0.1.tar.gz and save it to the local file /downloads/somefile-0.1.tar.gz.
- 2. Call the ftp_connect() function to connect to the FTP site; ftp_login() calls to login to the site anonymously.
- 3. Turn passive mode off using ftp_pasv(). Proceed to open a local file handle for the downloaded file, in this case the temporary file defined as /tmp/ftp.temp.
- 4. Employ the ftp_fget() function to save the desired remote file to the temporary file, using binary mode (FTP_IMAGE). To download a text file in ASCII mode, use FTP_ASCII.
- 5. Once downloaded, close the file handle and disconnect from the FTP site.
- 6. Move the temporary file to your local destination using the rename() function.
You could avoid some of these steps, particularly step six, by downloading directly to the final destination and skipping the temporary file altogether. However, using a temporary file is good practice because it a allows you to process the file before placing it in a location that may be public, which may be the case if a user employed a script on your site to download the file.
Just the beginning
I have just covered very basic usage of common protocols in PHP. If your PHP install has cURL support, you have a lot of other powerful commands that can be used to handle the HTTP protocol to both receive and transmit HTTP data. The SMTP support in PHP is simple; the mail() function pretty much does it all.
Of course, PHP can use more than just SMTP, HTTP, and FTP. Future articles will cover using PHP with SNMP, IMAP, POP3, and NNTP, among others.