Retrieving a Web Page Using fsockopen() : Socket « Network « PHP






Retrieving a Web Page Using fsockopen()

<html>
<head>
<title>Retrieving a Web page using fsockopen()</title>
</head>
<body>
<?php
$host = "www.java2s.com";
$page = "/index.htm";
$fp = fsockopen( "$host", 80, &$errno, &$errdesc);
if ( ! $fp )
    die ( "Couldn't connect to $host:\nError: $errno\nDesc: $errdesc\n" );
$request = "GET $page HTTP/1.0\r\n";
$request .= "Host: $host\r\n";
$request .= "Referer: http://www.java2s.com/index.htm\r\n";
$request .= "User-Agent: PHP test client\r\n\r\n";
$page = array();
fputs ( $fp, $request );
while ( ! feof( $fp ) )
    $page[] = fgets( $fp, 1024 );
fclose( $fp );print "the server returned ".(count($page))." lines!";
 ?>
</body>
</html>

           
       








Related examples in the same category

1.Open socket and read
2.Getting and Printing a Web Page with fopen()
3.Outputting the Status Lines Returned by Web Servers