PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 3. (Web hosting unlimited bandwidth)

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 3. First of all, you need to prepare a request and send it to the remote server. Add the following code after the comment in the else clause: // otherwise communicate with remote server // prepare the request $out = “GET /news.php HTTP/1.1rn”; $out .= “Host: www.friendsofed.comrn”; $out .= “Connection: Closernrn”; // send the request fwrite($remote, $out); The request is stored in $out and consists of the following three elements: The page you want, presented in this format: GET /path_to_page HTTP/1.1 The URL that we plan to open is www.friendsofed.com/news.php, which becomes just /news.php. Note that it begins with a forward slash. If you want the default page of a site, use a forward slash on its own. Host, followed by a colon and the domain name. An instruction to close the connection after the response has been sent. Each part of the request must be followed by a carriage return and new line character (rn), and the final line by an extra carriage return and new line character (rnrn). Since these characters are PHP escape sequences, you need to use double quotes (see Using escape sequences with double quotes in Chapter 3). Once you have built the request, send it by passing $out to fwrite() with a reference to the remote connection that you have opened. 4. After sending the request, you need to capture the response in a variable, and then close the socket connection. Add the following code to the else clause immediately below the code in the previous step: // initialize a variable to capture the response $received = ‘’; // keep the connection open until the end of the response while (!feof($remote)) { $received .= fgets($remote, 1024); } // close the connection fclose($remote); This uses feof(), fgets(), and fclose() in the same way as with local files. The only difference is that I have added a second argument to fgets(). This tells the function how many bytes to retrieve at a time. The fgets() function gets one line at a time, but some XML files don t use new lines, so it s more resource-efficient to specify a length. 5. Finally, use echo to display the response from the remote server. Add the following line after the closing curly brace of the conditional statement: echo $received;
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.

Leave a Reply