PHP/Network/Socket — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:06, 26 мая 2010
Содержание
Getting and Printing a Web Page with fopen()
<source lang="html4strict">
<html> <head> <title>Getting and printing a web page with fopen()</title> </head> <body> <?php $webpage = "http://www.wbex.ru/index.htm"; $fp = fopen( $webpage, "r" ) or die("couldn"t open $webpage"); while ( ! feof( $fp )){
print fgets( $fp, 1024 );
} ?> </body>
</html> </source>
Open socket and read
<source lang="html4strict">
<?php
// Establish a port 80 connection with www.example.ru $http = fsockopen("www.wbex.ru",80); // Send a request to the server $req = "GET / HTTP/1.1\r\n"; $req .= "Host: www.wbex.ru\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);
?>
</source>
Outputting the Status Lines Returned by Web Servers
<source lang="html4strict">
<html> <head> <title>Outputting the status lines returned by web servers</title> </head> <body> <?php $to_check = array ("www.wbex.ru" => "/index.htm"); foreach ( $to_check as $host => $page ){
$fp = fsockopen( "$host", 80, &$errno, &$errdesc, 10); print "Trying $hostprint "
\n"; if ( ! $fp ){ print "Couldn"t connect to $host:\n
Error: $errno\n
Desc:$errdesc\n";
\n";
continue; } print "Trying to get $page
\n"; fputs( $fp, "HEAD $page HTTP/1.0\r\n\r\n" ); print fgets( $fp, 1024 ); print "
\n"; fclose( $fp );
} ?> </body> </html>
</source>
Retrieving a Web Page Using fsockopen()
<source lang="html4strict">
<html> <head> <title>Retrieving a Web page using fsockopen()</title> </head> <body> <?php $host = "www.wbex.ru"; $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.wbex.ru/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>
</source>