[ Team LiB ] Previous Section Next Section

Getting a Document from a Remote Address

Although 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()
 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.


Prior to PHP 4.0.5, fopen() did not support HTTP redirects. When most modern browsers are sent a 301 or 302 response header, they make a new request based on the contents of the Location header. fopen() now supports this, so URLs that reference directories no longer have to end with a forward slash.

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


You can also access remote files using the include() statement. If the allow_url_fopen directive is set to On and a valid URL is passed to include(), then the result of a request for the remote file is incorporated into the script.

Unless you are very sure about what you are doing, you should be cautious of this feature. Including source code from third parties in your own project is a big security risk.

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 ] Previous Section Next Section