The example in this article describes how PHP implements a method of imitating a socket request to return a page. Share it with everyone for your reference. The specific implementation method is as follows:
$r = @file_get_contents($url);//What I returned here Is the number of pages. If there is no next page, return 0 and stop execution
echo $r;
if($r > 0)
{
$url = 'test.com/1 .php?p='.$r;
test($url);
}
else
{
echo 'fail';
}
}
$url = 'test.com/1.php?p=1';
$res = test($url);
?>
php uses socket to obtain web content
function GETBySocket($URL, $port=80) {
//get host from url
preg_match('/\/\/.*\//sU',$URL,$host_array);
if(!$host_array[0]) {
$URL.='/';
preg_match('/\/\/.*\//sU',$URL,$host_array);
}
$host=substr($host_array[0],2,-1);
//connect
$fp = stream_socket_client("$host:$port", $errcode, $errstr, 1);// or die("get ". $host ." failed");
//
$header = "GET ". $URL. " HTTP/1.1\r\n";
$header .= "Accept: */*\r\n";
$header .= "Accept-Language: zh-cn\r\n";
//$header .= "HTTP_CONNECTION: Keep-Alive\r\n";
$header .= "HTTP_ACCEPT: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n";
$header .= "HTTP_ACCEPT_CHARSET: gbk,*,utf-8\r\n";
//$header .= "Accept-Encoding: gzip, deflate\r\n";
$header .= "User-Agent: Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1;)\r\n";
$header .= "Host: ". $host ."\r\n";
//$header .= "Connection: Keep-Alive\r\n";
//$header .= "Cookie: cnzz02=2; rtime=1; ltime=1148456424859; cnzz_eid=566......余下全文>>