Internet Protocols: The web as most developers know it runs on HTTP and TCP-IP protocols. Web developers also use FTP protocols to transfer files to and from the server. We're also going to send and recieve email, using SMTP and POP / IMAP protocols.
Mail protocols: We use Simple Mail Transfer Protocol (SMTP) to send mail via the php mail() function. Several php add-ons use this (see chapter 30). To receive mail, use either Internet Message Access Protocol (IMAP) or Post Office Protocol (POP). Note that SMTP only sends, and cannot be used to receive mail, and IMAP and POP cannot send mail. IMAP reads and allows you to manipulate messages, and is more sophisticated than POP. POP allows you to download mail from the server, deleting it from the server. More on IMAP in chapter 29.
Information from other websites: Listing 19.1 (p. 421-2) uses file_get_contents ($my_url) to "screen scrape" the contents from a known page and to search the contents for a specific pattern (here, it's assumed that there is only one possible pattern to be matched on the page) using
$pattern = '(\\$[0-9 ]+\\.[0-9]+)';
if (eregi($pattern, $contents, $quote))
{ echo ...$quote[1];... }
Here, the example was to grab a stock market quote, but you could also get local weather data, etc. Note the warnings (p. 423) to consider intellectual property rights when you are taking information from a copyrighted page.
Beyond screen-scraping, see chapter 33, "Connecting to Web Services with XML and SOAP."
Network lookup functions: Listings 19.2 and 19.3 illustrate a form that specifies a URL and an email address, and the use of gethostbyname() and dns_get_mx() to check for the existence of a server at the URL and a mail server at the email address. The URL is broken down by parse_url() into an array of parts for scheme, user, pass, host, port, path, query, and fragments (p. 427) and the host component is checked, generating an IP if it is real:
$url = parse_url($passed_url);
$host=$url['host'];
$ip=gethostbyname($host);
Alternatively, if you know the IP, you can use gethostbyaddr() to get the hostname.
Similarly, you can parse the email address and check the mail host:
$email = explode('@', $passed_email);
$emailhost=$email[1];
dns_get_mx($emailhost, $mxhostarr);
which returns an array of mail hosts; if there is nothing returned, there is no place for the email to go. Note that dns_get_mx() isn't implemented in windows; use the PEAR::Net_DNS package instead.
File Transfer Protocol: Listing 19.4 (p. 428-435) uses PHP's FTP function to develop a mirror for a file; a mirror is a replicate file stored on another drive or host machine. The steps accomplish what you would have to do with any ftp server to download a file,
- Connect to the ftp server—use $conn=ftp_connect($host); to get a connection object
- login, providing an ftp user id and password—use $result = ftp_login($conn, $user, $password); to connect to the server.
- check whether the remote file has been updated (p. 433)
- download it—use $fp = fopen ($localfile, 'w'); to open the file and ftp_fget ($conn, $fp, $remotefile, FTP_BINARY); to get a binary copy of the file (or use FTP_ASCII for a text file).
- close the connection—ftp_quit('$conn);
To upload a file, use ftp_fput() and ftp_put() (p. 435). For larger files or slower connections, you can alter the time limit on file transfers using set_time_limit() (p. 436).