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'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!