Retrieving a Web Page Using fsockopen() : fsockopen « File Directory « PHP






Retrieving a Web Page Using fsockopen()

 
<html>
<head>
<title>Retrieving a Web page using fsockopen()</title>
</head>
<body>
<div>
<?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/page.html\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!";
?>
</div>
</body>
</html>
  
  








Related examples in the same category

1.Outputting the Status Lines Returned by Web Servers
2.fsockopen() function establishes a socket connection, TCP or UDP.
3.fsockopen.php
4.SSL Streams
5.Sockets Are Files
6.Scan a server