1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P78 pps

5 218 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 110,47 KB

Nội dung

17 Using Network and Protocol Functions IN THIS CHAPTER ,WE’ LL LOOK AT the network-oriented functions in PHP that enable your scripts to interact with the rest of the Internet.There’s a world of resources out there, and a wide variety of protocols available for using them. In this section we’ll con- sider n An overview of available protocols n Sending and reading email n Using other Web sites via HTTP n Using network lookup functions n Using FTP n Using generic network communications with cURL Overview of Protocols Protocols are the rules of communication for a given situation. For example, you know the protocol when meeting another person:You say hello, shake hands, communicate for a while, and then say goodbye. Computer networking protocols are similar. Like human protocols, different computer protocols are used for different situations and applications.We use HTTP, the Hypertext Transfer Protocol, for sending and receiv- ing Web pages.You will probably also have used FTP, File Transfer Protocol, for transfer- ring files between machines on a network.There are many others. Protocols, and other Internet Standards, are described in documents called RFCs,or Requests for Comments.These protocols are defined by the Internet Engineering Task Force (IETF).The RFCs are widely available on the Internet.The base source is the RFC Editor at http://www.rfc-editor.org/ 22 525x ch17 1/24/03 3:40 PM Page 357 358 Chapter 17 Using Network and Protocol Functions If you have problems when working with a given protocol, the RFCs are the authorita- tive source and are often useful for troubleshooting your code.They are, however, very detailed, and often run to hundreds of pages. Some examples of well-known RFCs are RFC2616, which describes the HTTP/1.1 protocol, and RFC822, which describes the format of Internet email messages. In this chapter, we will look at aspects of PHP that use some of these protocols. Specifically, we will talk about sending mail with SMTP, reading mail with POP and IMAP, connecting to other Web servers via HTTP and HTTPS, and transferring files with FTP. Sending and Reading Email The main way to send mail in PHP is to use the simple mail() function.We discussed the use of this function in Chapter 4,“String Manipulation and Regular Expressions,” so we won’t visit it again here.This function uses SMTP (Simple Mail Transfer Protocol) to send mail. You can use a variety of freely available classes to add to the functionality of mail(). In Chapter 28,“Building a Mailing List Manager,” we will use an add-on class to send HTML attachments with a piece of mail. SMTP is only for sending mail.The IMAP (Internet Message Access Protocol, described in RFC2060) and POP (Post Office Protocol, described in RFC1939 or STD0053) protocols are used to read mail from a mail server.These protocols cannot send mail. IMAP is used to read and manipulate mail messages stored on a server, and is more sophisticated than POP, which is generally used simply to download mail messages to a client and delete them from the server. PHP comes with an IMAP library.This can also be used to make POP and NNTP (Network News Transfer Protocol) as well as IMAP connections. We will look extensively at the use of the IMAP library in the project described in Chapter 27,“Building a Web-Based Email Service.” Using Other Web Sites One of the great things you can do with the Web is use, modify, and embed existing services and information into your own pages. PHP makes this very easy. Let’s look at an example to illustrate this. Imagine that the company you work for would like a stock quote for your company displayed on its homepage.This information is available out there on some stock exchange site somewhere—but how do we get at it? Start by finding an original source URL for the information.When you know this, every time someone goes to your home page, you can open a connection to that URL, retrieve the page, and pull out the information you require. 22 525x ch17 1/24/03 3:40 PM Page 358 359 Using Other Web Sites As an example, we’ve put together a script that retrieves and reformats a stock quote from the AMEX Web site. For the purpose of the example, we’ve retrieved the current stock price of Amazon.com. (The information you want to include on your page might differ, but the principles are the same.) This script is shown in Listing 17.1. Listing 17.1 lookup.php—Script Retrieves a Stock Quote from the NASDAQ for the Stock with the Ticker Symbol Listed in $symbol <html> <head> <title>Stock Quote from NASDAQ</title> </head> <body> <?php // choose stock to look at $symbol='AMZN'; echo "<h1>Stock Quote for $symbol</h1>"; $theurl='http://www.amex.com/equities/listCmp/EqLCDetQuote.jsp?Product_Symbol=AMZN '; if (!($fp = fopen($theurl, 'r'))) { echo 'Could not open URL'; exit; } $contents = fread($fp, 1000000); fclose($fp); //echo $contents; // find the part of the page we want and output it $pattern = "(\\\$[0-9 ]+\\.[0-9]+)"; if (eregi($pattern, $contents, $quote)) { echo "$symbol was last sold at: "; echo $quote[1]; } else { echo 'No quote available'; }; // acknowledge source echo '<br />' .'This information retrieved from <br />' 22 525x ch17 1/24/03 3:40 PM Page 359 360 Chapter 17 Using Network and Protocol Functions ."<a href=\"$theurl\">$theurl</a><br />" .'on '.(date('l jS F Y g:i a T')); ?> </body> </html> The output from one sample run of Listing 17.1 is shown in Figure 17.1. Listing 17.1 Continued Figure 17.1 The script uses a regular expression to pull out the stock quote from information retrieved from the stock exchange. The script itself is pretty straightforward—in fact, it doesn’t use any functions we haven’t seen before, just new applications of those functions. You might recall that when we discussed reading from files in Chapter 2,“Storing and Retrieving Data,” we mentioned that you could use the file functions to read from an URL.That’s what we have done in this case.The call to fopen() $fp = fopen($theurl, 'r') returns a pointer to the start of the page at the URL we supply.Then it’s just a question of reading from the page at that URL and closing it again: $contents = fread($fp, 1000000); fclose($fp); You’ll notice that we used a really large number to tell PHP how much to read from the file.With a file on the server, you’d normally use filesize($file),but this doesn’t work with a URL. When we’ve done this, we have the entire text of the Web page at that URL stored in $contents.We can then use a regular expression and the eregi() function to find the part of the page that we want: $pattern = "(\\\$[0-9 ]+\\.[0-9]+)"; if (eregi($pattern, $contents, $quote)) { 22 525x ch17 1/24/03 3:40 PM Page 360 361 Using Network Lookup Functions echo "$symbol was last sold at: "; echo $quote[1]; } That’s it! You can use this approach for a variety of purposes.Another good example is retriev- ing local weather information and embedding it in your page. The best use of this approach is to combine information from different sources to add some value. One good example of this approach can be seen in Philip Greenspun’s infa- mous script that produces the Bill Gates Wealth Clock: http://www.webho.com/WealthClock This page takes information from two sources. It obtains the current U.S. population from the U.S. Census Bureau’s site. It looks up the current value of a Microsoft share and combines these two pieces of information, adds a healthy dose of the author’s opinion, and produces new information—an estimate of Bill Gates’s current worth. One side note: If you’re using an outside information source such as this for a com- mercial purpose, it’s a good idea to check with the source first.There are intellectual property issues to consider in some cases. If you’re building a script like this, you might want to pass through some data. For example, if you’re connecting to an outside URL, you might like to pass some parame- ters typed in by the user. If you’re doing this, it’s a good idea to use the urlencode() function.This will take a string and convert it to the proper format for a URL, for example, transforming spaces into plus signs.You can call it like this: $encodedparameter = urlencode($parameter); One problem with this overall approach is that the site you’re getting the information from may change their data format which will stop your script from working. A better way of doing the same thing has recently begun being used—Web Services. These are like remote objects that you can connect to in order to retrieve data such as stock quotes. PHP’s unofficial support for Web Services is growing, and an official SOAP extension is in the works. You can find more information about Web Services in Chapter 31,“Connecting to Web Services with XML and SOAP.” Using Network Lookup Functions PHP offers a set of “lookup” functions that can be used to check information about hostnames, IP addresses, and mail exchanges. For example, if you were setting up a direc- tory site such as Yahoo! when new URLs were submitted, you might like to automati- cally check that the host of a URL and the contact information for that site are valid. This way, you can save some overhead further down the track when a reviewer comes to look at a site and finds that it doesn’t exist, or that the email address isn’t valid. Listing 17.2 shows the HTML for a submission form for a directory like this. 22 525x ch17 1/24/03 3:40 PM Page 361 . reading mail with POP and IMAP, connecting to other Web servers via HTTP and HTTPS, and transferring files with FTP. Sending and Reading Email The main way to send mail in PHP is to use the simple. sending and receiv- ing Web pages.You will probably also have used FTP, File Transfer Protocol, for transfer- ring files between machines on a network.There are many others. Protocols, and other. Email Service.” Using Other Web Sites One of the great things you can do with the Web is use, modify, and embed existing services and information into your own pages. PHP makes this very easy.

Ngày đăng: 07/07/2014, 03:20

TỪ KHÓA LIÊN QUAN