一小子攻城狮
想与博主以及众群友“发生关系”的请点击加【群243143941】;
  • 攻城狮
  • 身处网络
  • 随笔
7月12015

模拟GET或者POST请求测试CDN等

作者:老温   发布:2015-7-1 14:51 Wednesday   分类:攻城狮   阅读:119次   评论:0条  

/*
下面是一个模拟get或者post请求的方法支持
1.get,post方法
2.自定义参数
3.自定义header
4.返回服务器的返回内容和header
5.支持相特定的服务器请求url,适合测试cdn节点
*/
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){
	//根据返回的类型,修改header
	if($rheaders['Content-Type']!="")header('Content-Type: '.$rheaders['Content-Type']);
	echo $conntent;
}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;
			}
	}
}



/*
 * 模拟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";
	}
	//发送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);
}
?>


本文固定链接: http://php.oneboys.cn/post-47.html

blogger
该日志由 老温 于2015-7-1 14:51 Wednesday发表在 攻城狮 分类下。
版权所有:《一小子攻城狮》 → 《模拟GET或者POST请求测试CDN等》;
除特别标注,本博客所有文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签: PHP CDN测试
上一篇::SQL 删除重复记录,并保留其中一条
下一篇:CSS自动折行换行全兼容

热门文章

相关文章

  • linux wget 安装RAR
  • PDO实现mysql_ping的功能
  • 网页判断是否关注公众号
  • curl下载文件
  • emlog之nginx伪静态重写规则
取消回复

发表评论

亲,头像对么?

提交中,请稍候……



    最新文章热门文章随机文章

    • php邀请背景图合成二维码
    • 列表点击编辑
    • css 宽度不固定 正方形盒子
    • 终端生成证书 公私钥
    • apache环境接收自定义header
    • PHP curl 模块获取header和body完整信息
    • php导出excel,不用php Excel类
    • Mysql 导入错误1064 USING BTREE错误
    • PHP PDO属性列表以及PDO方法分类
    • mysql1153错误,max_allowed_packet
    • SEO优化旗舰店站,js展示店铺内容技巧
    • SVN命令文档
    • php取客户端IP
    • sql语句随机取出数据
    • QQ群排名技术
  • 标签

      PDO方法 PDO属性 MYSQL MYSQL重装 linux学习 linux命令screen php命令行 phpExcel max_allowed_packet Jquery php命名空间 MYSQL错误 php获取header信息 getallheaders php弱类型 php运算符优先级 自媒体运营 svn svn命令 解除svn控制 PHP 客户端IP sql随机取出数据 SQL语句limit qq群排名 QQ群排名技术 刷QQ群活跃度 .htaccess rar安装 301重定向 mysql_ping api错误码设计 PDO PDO连接状态 营销中的驱动媒介 curl获取header信息 CURL HTML+CSS checkbox的change事件 DOM加载 图片加载 微信公众账号 微信公众账号加粉 解除svn版本控制 html5 html5上传 html5进度条上传 高并发 队列 高并发超载 html5预览 MEMCACHE 一致性哈希算法 WDCP zendStudio php内置函数不能自动提示 insertinto selectinto 经典SQL语句大全 sql学习 Javascript 日期函数 获取月份天数 linux find命令 锚点 HTML 正则表达式 CSS自动折行 CSS自动换行 SQL CDN测试 哈夫曼 SQL删除重复并保留一条 curl下载 修改mysql root密码 php删除文件夹 php遍历文件夹 JS js时间戳 iframe跨域 js+iframe跨域传递参数 长连接 mysql长连接apache mysql长连接fastcgi base64编码加密 SSDB ssdb遍历集合 ssdb集合 emlog nginx伪静态 Fireworks 文字水印 前端 echarts echarts全国地图 html+css隐藏滚动条 魔方图片 魔方相册 html+css魔方相册 php错误日志 PhpSpreadsheet PhpSpreadsheet导入 PhpSpreadsheet导出 PhpSpreadsheet设置单元格属性 js函数抖动 中文分词 中文切词 php公众号 网易云音乐播放器 网易云音乐歌单播放器 解析header 解析cookie CSS
  • 存档

    • 2021年2月(2)
    • 2021年1月(3)
    • 2020年11月(11)
    • 2020年10月(3)
    • 2020年8月(1)
    • 2020年2月(9)
    • 2020年1月(2)
    • 2019年12月(3)
    • 2016年11月(1)
    • 2016年5月(1)
    • 2016年2月(1)
    • 2015年10月(2)
    • 2015年9月(2)
    • 2015年7月(9)
    • 2015年6月(5)
    • 2015年5月(1)
    • 2015年4月(4)
    • 2015年3月(13)
    • 2015年2月(3)
    • 2015年1月(8)
    • 2014年12月(10)
© 2010 - 2014 老温PHP 版权所有