时间:2021-07-01 10:21:17 帮助过:1人阅读
本文实例讲述了PHP实现模仿socket请求返回页面的方法。分享给大家供大家参考。具体实现方法如下:
代码如下:<?php
$url = "www.XXXX.com"; //自己做替换
$parse = parse_url($url); //对URL进行解析,返回起组成部分。
$host = $parse['host'];
$path = $parse['path'];
$port = 80;
$timeout = 80;
$fp = @fsockopen($host, $port, $errno, $errstr, $timeout); //打开socket链接
if (!$fp){
echo $errno."--".$errstr; //如果错误,则返回错误代码和错误信息
} else {
$out = "POST $path HTTP/1.1\r\n"; //以下是HTTP请求头信息
$out .= "Host: ".$host."\r\n";
$out .= "Accept: */*\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
@fwrite($fp, $out); //把请求信息写到链接中
$status = stream_get_meta_data($fp);
if(!$status['timed_out']) {
while (!feof($fp)) {
if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n")) {
break;
}
}
$stop = false;
while(!feof($fp) && !$stop) {
$data = fread($fp,8192); //8192为可返回字节数
$return .= $data;
}
}
fclose($fp);
print_r($return);
}
希望本文所述对大家的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......余下全文>>
http://www.bkjia.com/PHPjc/906115.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/906115.htmlTechArticlePHP实现模仿socket请求返回页面的方法,phpsocket 本文实例讲述了PHP实现模仿socket请求返回页面的方法。分享给大家供大家参考。具体实现方法...