This
section demonstrates how this is accomplished, using the fsockopen() function. Its
prototype follows:
resource fsockopen(string target, int port [, int errno [, string errstring
[, float timeout]]])
CHAPTER 16 ?– NETWORKING 409
The fsockopen() function establishes a connection to the resource designated by
target on port port, returning error information to the optional parameters errno
and errstring. The optional parameter timeout sets a time limit, in seconds, on how
long the function will attempt to establish the connection before failing.
The first example shows how to establish a port 80 connection to www.example.com
using fsockopen() and how to output the index page:
// Establish a port 80 connection with www.example.com
$http = fsockopen("www.example.com",80);
// Send a request to the server
$req = "GET / HTTP/1.1\r\n";
$req .= "Host: www.example.com\r\n";
$req .= "Connection: Close\r\n\r\n";
fputs($http, $req);
// Output the request results
while(!feof($http)) {
echo fgets($http, 1024);
}
// Close the connection
fclose($http);
?>
This returns the following:
HTTP/1.1 200 OK Date: Mon, 09 Oct 2006 23:33:52 GMT Server: Apache/2.0.54 (Fedora)
(Red-Hat/Linux) Last-Modified: Wed, 15 Nov 2005 13:24:10 GMT ETag:
"63ffd-1b6-80bfd280" Accept-Ranges: bytes Content-Length: 438
Connection: close Content-Type: text/html
You have reached this web page by typing "example.
Pages:
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490