Home  >  Article  >  php教程  >  Detailed explanation of php source code fsockopen to obtain web page content examples

Detailed explanation of php source code fsockopen to obtain web page content examples

高洛峰
高洛峰Original
2016-12-21 16:06:571464browse

PHP fsockopen function description:

Open Internet or Unix domain socket connection (open socket link)

Initiates a socket connection to the resource specified by target .

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ). It returns a file handle

Open the PHP fsockopen function

PHP fsockopen requires PHP.ini The allow_url_fopen option is enabled.

Use fsockopen to obtain web content

The specific source code is as follows:

<?php
$host = "www.manongjc.com";
$page = "/index.htm";
$fp = fsockopen( "$host", 80, $errno, $errdesc );
if ( ! $fp ) {
 die ( "Couldn&#39;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.manongjc.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!";
?>

The above is the detailed knowledge of php source code fsockopen to obtain web content examples. Friends in need can refer to it. Thank you for your support to this site. !

For more php source code, fsockopen obtains web content examples for detailed explanation. Please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn