[ Team LiB ] |
![]() ![]() |
Getting a Document from a Remote AddressAlthough PHP is a server-side language, it can act as a client, requesting data from remote servers and making the output available to your scripts. If you are already comfortable reading files from the server, you will have no problem using PHP to acquire information from the Web. In fact, the syntax is exactly the same. You can use fopen() to connect to a Web address in the same way as you would with a file. Listing 14.4 opens a connection to a remote server and requests a page, printing the result to the browser. Listing 14.4 Getting and Printing a Web Page with fopen()1: <!DOCTYPE html PUBLIC 2: "-//W3C//DTD XHTML 1.0 Strict//EN" 3: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 4: <html> 5: <head> 6: <title>Listing 14.4 Getting a Web Page with fopen()</title> 7: </head> 8: <body> 9: <div> 10: <?php 11: $webpage = "http://p24.corrosive.co.uk:9090/source/readthis.php"; 12: $fp = fopen( $webpage, "r" ) or die("couldn't open $webpage"); 13: while ( ! feof( $fp )) { 14: print fgets( $fp, 1024 ); 15: } 16: ?> 17: </div> 18: </body> 19: </html> To take advantage of this feature, you need to ensure that the allow_url_fopen directive is set to On. This is the default setting. You most likely won't want to output an entire page to the browser. More commonly, you would parse the document you download.
fopen() returns a file resource if the connection is successful and false if the connection cannot be established or the page doesn't exist. After you have a file pointer, you can use it as normal to read the file. PHP introduces itself to the remote server as a client. On my system, it sends the following request: GET /source/readthis.php HTTP/1.0 Host: p24.corrosive.co.uk:9090
This process is simple and is the approach you will use to access a Web page in most instances. There is more to fopen() than we have covered yet. We look again at the function in the section "An Introduction to Streams," later in this chapter. ![]() |
[ Team LiB ] |
![]() ![]() |