下面是一个模拟get或者post请求的方法支持
1.get,post方法
2.自定义参数
3.自定义header
4.返回服务器的返回内容和header
5.支持相特定的服务器请求url,适合测试cdn节点
代码如下
- <?php
-
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());
-
-
-
if($rheaders["httpstatus"]>=200&&$rheaders["httpstatus"]<=300)
- {
-
-
-
if($rheaders['Content-Type']!="")
-
header('Content-Type: '.$rheaders['Content-Type']);
-
echo $conntent;
-
-
}else
- {
-
-
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)
- {
-
-
if($rheaders['Content-Type'])
-
header('Content-Type: '.$rheaders['Content-Type']);
-
echo $conntent;
- }
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
function request_url($ip,$port,$method,$aURL,$headers,$aParamsArray)
- {
-
$rheaders=array();
-
$rheaders["httpstatus"]=0;
-
-
$URL=parse_url($aURL);
-
if(!$port)
- {
-
-
$port=($URL["port"]? $URL["port"]:80);
- }
-
if(strcasecmp($method,"get")==0){
-
$method="GET";
-
}else{
-
$method="POST";
- }
-
-
foreach($aParamsArray as $key=> $value)
- {
-
if($flag!=0)
- {
-
$params.="&";
-
$flag=1;
- }
-
$params.=$key."=";
-
$params.=urlencode($value);
-
$flag=1;
- }
-
if($method=="POST")
- {
-
-
$length=strlen($params);
-
-
}else
- {
-
$length=0;
-
-
$URL["query"]=$URL["query"]."&".$params;
- }
-
if($URL["query"]){
-
-
$URL["path"].="?".$URL["query"].($URL["fragment"]?"#".$URL["fragment"]:"");
- }
-
-
$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']);
-
-
$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")
- {
-
-
$header.=$params."\r\n";
- }
-
-
-
fputs($fp,$header);
-
$inheader=1;
-
$lineno=0;
-
$conntent="";
-
-
while(!feof($fp))
- {
-
if($inheader){
-
$line=fgets($fp,1024);
-
}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];
- }
- }
-
-
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);
- }
- ?>
本文转自yifangyou 51CTO博客,原文链接:http://blog.51cto.com/yifangyou/604613,如需转载请自行联系原作者