[ Pobierz całość w formacie PDF ]
.Listing 13.4: Getting and Printing a Web Page with fopen()1:2:3: Listing 13.4 Getting and printing a web page with fopen()4: 2445:6:12:13:When this script is run, you should see the PHP homepage.Notice that the imageson the page we include are "broken." This is because paths to images in IMGelements are often relative.When our script outputs the data it has downloaded, theuser's browser will look on our server for the images referenced.We could workaround this problem to some extent by adding the following tag to the HEADelement of our script:It is unlikely that you will want to output an entire page to the browser.Morecommonly, you would parse the document you download.fopen() returns a file pointer if the connection is successful and false if theconnection cannot be established or the page doesn't exist.After you have a filepointer, you can use it as normal to read the file.PHP introduces itself to the remoteserver as a client.On my system, it sends the following request:GET / HTTP/1.0Host: www.php.netUser-Agent: PHP/4.0b3This process is simple and is the approach you will use to access a Web page in mostinstances.You might want to connect to other network services, however, or learnmore about a Web document by parsing the server headers.You will look at how todo this later in the hour.Converting IP Addresses and HostnamesEven if your server does not provide you with a $REMOTE_HOST variable, you willprobably know the IP address of a visitor from the $REMOTE_ADDR environmentalvariable.You can use this in conjunction with the function gethostbyaddr() to getthe user's hostname.gethostbyaddr() requires a string representing an IP address.It returns the equivalent hostname.If an error occurs, it returns the IP address it 245was given.Listing 13.5 creates a script that uses gethostbyaddr() to acquire theuser's hostname if the $REMOTE_HOST variable is not available.Listing 13.5: Using gethostbyaddr() to Get a Hostname1:2:3: Listing 13.5 Using gethostbyaddr() to get a host name4:5:6:14:15:If we have access to the $REMOTE_HOST variable, we simply print this to thebrowser.Otherwise, if we have access to the $REMOTE_ADDR variable, we attemptto acquire the user's hostname using gethostbyaddr().If all else fails, we print ageneric welcome message.To attempt to convert a hostname to an IP address, you can use gethostbyname().This function requires a hostname as its argument.It returns an IP address or, if anerror occurs, the hostname you provided.Making a Network ConnectionSo far we have had it easy.This is because PHP makes working with a Web page ona remote server as simple as opening a file on your own system.Sometimes, though,you need to exercise a little more control over a network connection or acquire moreinformation about it.You can make a connection to an Internet server with fsockopen(), which requires ahostname or IP address, a port number, and two reference variables.Rememberthat you can pass a reference to a variable by prepending an ampersand (&) symbol.The reference variables you passed to fsockopen() are populated to provide more 246information about the connection attempt should it fail.You can also passfsockopen() an optional timeout integer, which determines how long fsockopen()will wait (in seconds) before giving up on a connection.If the connection issuccessful, a file pointer is returned.Otherwise, it returns false.The following fragment initiates a connection to a Web server:$fp = fsockopen( "www.corrosive.co.uk", 80, &$errno, &errdesc, 30 );80 is the usual port number that a Web server listens on.The first reference variable, $errno, contains an error number if the connection isunsuccessful, and $errdesc might contain more information about the failure.After you have the file pointer, you can both write to the connection with fputs() andread from it with fgets() as you might with a file.When you have finished workingwith your connection, you should close it with fclose().We now have enough information to initiate our own connection to a Web server.Listing 13.6 makes an HTTP connection, retrieving a page and storing it in avariable.Listing 13.6: Retrieving a Web Page Using fsockopen()1:2:3: Listing 13.6 Retrieving a Web page using fsockopen()4:5:6:23:24:Notice the request headers that we send to the server.The Webmaster at theremote host will see the value you sent in the User-Agent header in her log file.Shemay also assume that a visitor to our page connected from a link athttp://80-www.corrosive.co.uk.proxy.lib.uiowa.edu/refpage.html [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • agnieszka90.opx.pl