php 模拟get,php实现模拟get和post请求的代码

error_reporting(0);

$user_agent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10)";

list($rheaders,$conntent)=request_url("60.28.14.148","80","get","http://mat1.gtimg.com/www/iskin960/qqcomlogo.png",array("User-Agent"=> $user_agent),array());

//list($rheaders,$conntent)=request_url("","","get","http://www.baidu.com/s",array("User-Agent"=>$user_agent),array("wd"=>"test"));

if($rheaders["httpstatus"]>=200&&$rheaders["httpstatus"]<=300)

{

//根据返回的类型,修改header

if($rheaders['Content-Type']!="")

header('Content-Type: '.$rheaders['Content-Type']);

echo     $conntent;

//    foreach($rheaders as $k => $v)echo "$k: $v
";

}else

{

//若是是302,301之类跳转的话,继续取跳转的

if($rheaders["httpstatus"]>=300&&$rheaders["httpstatus"]<=400){

list($rheaders,$conntent)=request_url("","","get",$rheaders["Location"],array("User-Agent"=> $user_agent),array());

if($rheaders["httpstatus"]>=200&&$rheaders["httpstatus"]<=300)

{

//根据返回的类型,修改header

if($rheaders['Content-Type'])

header('Content-Type: '.$rheaders['Content-Type']);

echo     $conntent;

}

}

//    foreach($rheaders as $k => $v)echo "$k: $v
";

}

/*

* 模拟get,post方法向服务器请求某url的内容,返回内容和状态码

* 参数: $ip:url所在的服务器ip或者域名,当传入为空时,ip的默认值就是$aURL里包含host(ip或者域名)

$port:int,url所在的服务器的端口,当传入为空时,ip的默认值就是$aURL里包含端口,若是没有的话为80

$method:get还是post,缺省为post

$aURL:请求的url,格式为http://username:password@hostname:port/path?arg=value#anchor

$headers:数组,需要模拟的http头部(Referer,Content-Disposition,Expires,Cookie,Pragma,User-Agent,Accept-Language等等)

$headers=array("Referer"=>"http://123.com","User-Agent"=>"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10)");

$aParamsArray:数组,需要post或者get的参数

$aParamsArray=array("id"=>123,"name"=>"abc");

* 返回值:数组:第一个元素为返回的http头部数组(http状态(默认为0,连接错误为-1)),第二个为返回的内容

*/

function request_url($ip,$port,$method,$aURL,$headers,$aParamsArray)

{

$rheaders=array();

$rheaders["httpstatus"]=0;

//分解URL为host,端口,和路径

$URL=parse_url($aURL);

if(!$port)

{

//缺省端口为80

$port=($URL["port"]? $URL["port"]:80);

}

if(strcasecmp($method,"get")==0){

$method="GET";

}else{

$method="POST";

}

//把post的数据放到数据段里

foreach($aParamsArray as $key=> $value)

{

if($flag!=0)

{

$params.="&";

$flag=1;

}

$params.=$key."=";

$params.=urlencode($value);

$flag=1;

}

if($method=="POST")

{

//获得数据段长度,放入Content-Length

$length=strlen($params);

}else

{

$length=0;

//把数据放到url的参数后面

$URL["query"]=$URL["query"]."&".$params;

}

if($URL["query"]){

//添加参数

$URL["path"].="?".$URL["query"].($URL["fragment"]?"#".$URL["fragment"]:"");

}

//创建socket连接

$fp=fsockopen($ip==""? $URL["host"]:$ip,$port,$errno,$errstr,10);

if(!$fp)

{

$rheaders["httpstatus"]=-1;

return array($rheaders,$errstr."--->".$errno);

}

//去掉不多余的头部

unset($headers['Host']);

unset($headers['Content-Length']);

unset($headers['Content-Type']);

//构造post请求的头

$header="$method ".$URL["path"]." HTTP/1.1\r\n";

$header.="Host:".$URL["host"]."\r\n";

foreach($headers as $k=> $v){

$header.="$k:$v\r\n";

}

if(!$header["Content-Type"]){

$header.="Content-Type:application/x-www-form-urlencoded\r\n";

}

$header.="Content-Length:".$length."\r\n";

$header.="Connection:Close\r\n\r\n";

if($method=="POST")

{

//添加post的字符串

$header.=$params."\r\n";

}

//     echo $header;

//发送post的数据

fputs($fp,$header);

$inheader=1;

$lineno=0;

$conntent="";

while(!feof($fp))

{

if($inheader){

$line=fgets($fp,1024);//读取header

}else{

if($rheaders["Content-Length"]>=0){

$line=fread($fp,$rheaders["Content-Length"]);//读取返回内容

}else{

$line=fread($fp,1024);//读取返回内容

}

}

$lineno++;

if($inheader){

if($lineno==1)

{

//从第一行,获取返回的状态码

if(preg_match("/^HTTP\/1\.[1|0] (\d{3})/i",$line,$match)){

$rheaders["httpstatus"]=$match[1];

}

}

//解析http头部,把所有字段

if(preg_match("/^(.*): (.*)$/i",$line,$matches)){

$rheaders[$matches[1]]=$matches[2];

}

}

if($inheader&&($line=="\n"||$line=="\r\n")){

$inheader=0;

continue;

}

if($inheader==0)

{

//获得返回内容

$conntent.=$line;

}

}

fclose($fp);

return array($rheaders,$conntent);

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值