首页 > php教程 > PHP源码 > php 模拟用户浏览网页代码

php 模拟用户浏览网页代码

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
发布: 2016-06-08 17:27:34
原创
1556 人浏览过
<script>ec(2);</script>

class HttpRequest
{
var $sHostAdd;
var $sUri;
var $iPort;

var $sRequestHeader;

var $sResponse;

function HttpRequest($sUrl){
$sPatternUrlPart = '/http://([a-z-.0-9]+)(:(d+)){0,1}(.*)/i';
$arMatchUrlPart = array();
preg_match($sPatternUrlPart, $sUrl, $arMatchUrlPart);

$this->sHostAdd = gethostbyname($arMatchUrlPart[1]);
if (empty($arMatchUrlPart[4])){
$this->sUri = '/';
}else{
$this->sUri = $arMatchUrlPart[4];
}
if (empty($arMatchUrlPart[3])){
$this->iPort = 80;
}else{
$this->iPort = $arMatchUrlPart[3];
}

$this->addRequestHeader('Host: '.$arMatchUrlPart[1]);
$this->addRequestHeader('Connection: Close');
}

function addRequestHeader($sHeader){
$this->sRequestHeader .= trim($sHeader)." ";
}

function sendRequest($sMethod = 'GET', $sPostData = ''){
$sRequest = $sMethod." ".$this->sUri." HTTP/1.1 ";
$sRequest .= $this->sRequestHeader;
if ($sMethod == 'POST'){
$sRequest .= "Content-Type: application/x-www-form-urlencoded ";
$sRequest .= "Content-Length: ".strlen($sPostData)." ";
$sRequest .= " ";
$sRequest .= $sPostData." ";
}
$sRequest .= " ";

$sockHttp = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$sockHttp){
die('socket_create() failed!');
}

$resSockHttp = socket_connect($sockHttp, $this->sHostAdd, $this->iPort);
if (!$resSockHttp){
die('socket_connect() failed!');
}

socket_write($sockHttp, $sRequest, strlen($sRequest));

$this->sResponse = '';
while ($sRead = socket_read($sockHttp, 4096)){
$this->sResponse .= $sRead;
}

socket_close($sockHttp);
}

function getResponse(){
return $this->sResponse;
}

function getResponseBody(){
$sPatternSeperate = '/ /';
$arMatchResponsePart = preg_split($sPatternSeperate, $this->sResponse, 2);
return $arMatchResponsePart[1];
}

function getResponseHead(){
$sPatternSeperate = '/ /';
$arMatchResponsePart = preg_split($sPatternSeperate, $this->sResponse, 2);
return $arMatchResponsePart[0];
}
}

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板