码迷,mamicode.com
首页 > Web开发 > 详细

模拟http响应头

时间:2016-06-16 21:47:51      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

1.打开需要模拟的页面请求头

技术分享

2.编码要发送的数据

技术分享

3.通过fsockopen函数发送请求头

代码如下

//方法二
$URL="http://device.test.com/admin/manager/add";  
$post_data[‘account‘] = "test06";  
$post_data[‘password‘] = "test06";  
$post_data[‘name‘] = "武神"; 

foreach($post_data as $key=>$value)  
$values[]="$key=".urlencode($value);   
$data_string=implode("&",$values);  

$URL_Info=parse_url($URL);  
$referrer="http://device.test.com/admin/manager/add";  
//指定请求的url
if($referrer=="") 
$referrer=$_SERVER["SCRIPT_URI"];  
   
//端口号
if(!isset($URL_Info["port"]))  
$URL_Info["port"]=80;  

//请求头编写
$request = "";
$request.="POST ".$URL_Info["path"]." HTTP/1.1\n";  
$request.="Host: ".$URL_Info["host"]."\n";  
$request.="Referer: $referrer\n";  
$request.="Content-type: application/x-www-form-urlencoded\n";  
$request.="Cookie: ci_session=f8c16115464b90a7de13b1a0f14d6e1e9b9db05c\n";//看自己模拟网站是否需要session  
$request.="User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.76 Mobile Safari/537.36\n";  
$request.="Content-length: ".strlen($data_string)."\n";  
$request.="Connection: close\n";  
$request.="\n";  
$request.=$data_string."\n";  

//模拟发送post
$fp = fsockopen($URL_Info["host"],$URL_Info["port"]);  
fputs($fp, $request);  
while(!feof($fp)) {  
    $result .= fgets($fp, 128);  //从文件指针中读取一行。
}  
fclose($fp); 

 

模拟http响应头

标签:

原文地址:http://www.cnblogs.com/zc123/p/5592123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!