PHP用fsocket模拟GET和POST请求


PHP #fsocket2012-10-29 14:33

相关的实现代码如下:

模拟发出请求的页面index.php

<?php 
//fsocket模拟get提交
$gurl = "http://yige.org/php/t.php?uu=yige";
//print_r(parse_url($gurl));
echo "以下是GET方式的响应内容:<br>";
sock_get($gurl);
function sock_get($url)
{
	$info = parse_url($url);
	$fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
	$head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0\r\n";
	$head .= "Host: ".$info['host']."\r\n";
	$head .= "\r\n";
	$write = fputs($fp, $head);
	while (!feof($fp))
	{
		$line = fgets($fp);
		echo $line."<br>";
	}
}


//fsocket模拟post提交
$purl = "http://yige.org/php/t.php";
echo "以下是POST方式的响应内容:<br>";
sock_post($purl,"uu=rrrrrrrrrrrr&&kk=mmmmmm");
function sock_post($url, $query)
{
	$info = parse_url($url);
	$fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
	$head = "POST ".$info['path']." HTTP/1.0\r\n";
	$head .= "Host: ".$info['host']."\r\n";
	$head .= "Referer: http://".$info['host'].$info['path']."\r\n";
	$head .= "Content-type: application/x-www-form-urlencoded\r\n";
	$head .= "Content-Length: ".strlen(trim($query))."\r\n";
	$head .= "\r\n";
	$head .= trim($query);
	$write = fputs($fp, $head);
	while (!feof($fp))
	{
		$line = fgets($fp);
		echo $line."<br>";
	}
}
?> 


请求的响应页面t.php

<?php
if(isset($_GET['uu'])){
	echo '<font color="red">t.php中$_GET["uu"]的值是:'.$_GET['uu']."</font><br>";
}
if(isset($_POST['uu'])){
	echo '<font color="red">t.php中$_POST的值是:</font><br>';
	print_r($_POST);
}
?>


相关文章

粤ICP备11097351号-1