Use fsocket to simulate GET and POST requests (measured and easy to use)

[PHP]

<?php  
//fsocket模拟get提交  
$gurl = "http://localhost/php/t.php?uu=gggggg";  
//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>";  $purl = "http://localhost/php/t.php"; //fsocket simulates post submission }  
}  

 
 
 


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>";  
}  
}  
?>  

Requested response page t.php
[php]
<?php 
if(isset($_GET['uu'])){ 
    echo '<font color="red">The value of $_GET["uu"] in t.php is: '.$_GET['uu']."</font><br>"; 

if(isset($_POST['uu'])){ 
    echo '<font color="red">t.php The value of $_POST is: </font><br>'; 
    print_r($_POST); 
}    
?> 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325616777&siteId=291194637