PHP/Network/Socket — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
Getting and Printing a Web Page with fopen()
<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>
Open socket and read
<?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);
?>
Outputting the Status Lines Returned by Web Servers
<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 $host<BR>\n";
if ( ! $fp ){
print "Couldn"t connect to $host:\n<br>Error: $errno\n<br>Desc:$errdesc\n";
print "<br><hr><br>\n";
continue;
}
print "Trying to get $page<br>\n";
fputs( $fp, "HEAD $page HTTP/1.0\r\n\r\n" );
print fgets( $fp, 1024 );
print "<br><br><br>\n";
fclose( $fp );
}
?>
</body>
</html>
Retrieving a Web Page Using fsockopen()
<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>