PHP fsockopen函數說明:
Open Internet or Unix domain socket connection(打開套接字鏈接)
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() ).就是返回一個文件句柄
開啟PHP fsockopen這個函數
PHP fsockopen需要 PHP.ini 中 allow_url_fopen 選項開啟。
使用fsockopen獲取網頁內容
具體源代碼如下:
<?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!";
?>
以上就是php源碼 fsockopen獲取網頁內容實例詳解的知識,有需要的小伙伴可以參考下,謝謝大家對本站的支持!