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 "; $request.="Host: ".$URL_Info["host"]." "; $request.="Referer: $referrer "; $request.="Content-type: application/x-www-form-urlencoded "; $request.="Cookie: ci_session=f8c16115464b90a7de13b1a0f14d6e1e9b9db05c ";//看自己模拟网站是否需要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 "; $request.="Content-length: ".strlen($data_string)." "; $request.="Connection: close "; $request.=" "; $request.=$data_string." "; //模拟发送post $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); fputs($fp, $request); while(!feof($fp)) { $result .= fgets($fp, 128); //从文件指针中读取一行。 } fclose($fp);