13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

452 Chapter 20 Using Network <strong>and</strong> Protocol Functions<br />

Task Force (IETF).The RFCs are widely available on the Internet.The base source is<br />

the RFC <strong>Ed</strong>itor at http://www.rfc-editor.org/.<br />

If you have problems when working with a given protocol, the documents that define<br />

them are the authoritative sources <strong>and</strong> are often useful for troubleshooting your code.<br />

They are, however, very detailed <strong>and</strong> often run to hundreds of pages.<br />

Some examples of well-known RFCs are RFC2616, which describes the HTTP/1.1<br />

protocol, <strong>and</strong> RFC822, which describes the format of Internet email messages.<br />

In this chapter, we look at aspects of <strong>PHP</strong> that use some of these protocols.<br />

Specifically, we discuss sending mail with SMTP, reading mail with POP3 <strong>and</strong> IMAP4,<br />

connecting to other web servers via HTTP, <strong>and</strong> transferring files with FTP.<br />

Sending <strong>and</strong> Reading Email<br />

The main way to send mail in <strong>PHP</strong> is to use the simple mail() function.We discussed<br />

the use of this function in Chapter 4,“String Manipulation <strong>and</strong> Regular Expressions,” so<br />

we won’t visit it again here.This function uses the Simple Mail Transfer Protocol<br />

(SMTP) to send mail.<br />

You can use a variety of freely available classes to add to the functionality of mail().<br />

In Chapter 30,“Building a Mailing List Manager,” you use an add-on class to send<br />

HTML attachments with a piece of mail. SMTP is only for sending mail.The Internet<br />

Message Access Protocol (IMAP4), described in RFC2060, <strong>and</strong> Post Office Protocol<br />

(POP3), described in RFC1939 or STD0053, are used to read mail from a mail server.<br />

These protocols cannot send mail.<br />

IMAP4 is used to read <strong>and</strong> manipulate mail messages stored on a server <strong>and</strong> is more<br />

sophisticated than POP3, which is generally used simply to download mail messages to a<br />

client <strong>and</strong> delete them from the server.<br />

<strong>PHP</strong> comes with an IMAP4 library. It can also be used to make POP3 <strong>and</strong> Network<br />

News Transfer Protocol (NNTP) as well as IMAP4 connections.<br />

We look extensively at the use of the IMAP4 library in the project described in<br />

Chapter 29,“Building a <strong>Web</strong>-Based Email Service.”<br />

Using Data from Other <strong>Web</strong>sites<br />

One of the great things you can do with the <strong>Web</strong> is use, modify, <strong>and</strong> embed existing<br />

services <strong>and</strong> information into your own pages. <strong>PHP</strong> makes this very easy. Let’s look at an<br />

example to illustrate this use.<br />

Imagine that the company you work for wants the company’s stock quote displayed<br />

on its home page.This information is available on some stock exchange site somewhere,<br />

but how do you get at it?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!