Rumah >pembangunan bahagian belakang >tutorial php >PHP实现模仿socket请求返回页面的方法,phpsocket_PHP教程
本文实例讲述了PHP实现模仿socket请求返回页面的方法。分享给大家供大家参考。具体实现方法如下:
希望本文所述对大家的PHP程序设计有所帮助。
1.用js的setInterval+ajax来请求,PHP里面加条件判断是否成功;
2.用PHP递归循环来做,如:
function test($url)
{
echo $url.'\t\n--------';
$r = @file_get_contents($url);//我这里返回的是页数,如果没有下一页的时候返回0,停止执行
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);
?>
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......余下全文>>