模拟http响应头
Posted 东歌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟http响应头相关的知识,希望对你有一定的参考价值。
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响应头的主要内容,如果未能解决你的问题,请参考以下文章