首頁 >後端開發 >php教程 >利用fsocket模拟GET和POST请求_PHP教程

利用fsocket模拟GET和POST请求_PHP教程

WBOY
WBOY原創
2016-07-13 17:52:291122瀏覽

[php]
//fsocket模拟get提交  
$gurl = "http://localhost/php/t.php?uu=gggggg";  
//print_r(parse_url($gurl));  
echo "以下是GET方式的响应内容:
"; 
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."
";  
}  
}  
 
 
 
//fsocket模拟post提交 
$purl = "http://localhost/php/t.php"; 
echo "以下是POST方式的响应内容:
"; 
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."
";  
}  
}  
?>  

请求的响应页面t.php
[php]
if(isset($_GET['uu'])){ 
    echo 't.php中$_GET["uu"]的值是:'.$_GET['uu']."
"; 

if(isset($_POST['uu'])){ 
    echo 't.php中$_POST的值是:
'; 
    print_r($_POST); 
}    
?> 

以下为运行结果:

利用fsocket模拟GET和POST请求_PHP教程
以下为Firebug的查看结果:

 利用fsocket模拟GET和POST请求_PHP教程
作者:Fanteathy

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478114.htmlTechArticle[php] ?php //fsocket模拟get提交 $gurl = http://localhost/php/t.php?uu=gggggg; //print_r(parse_url($gurl)); echo 以下是GET方式的响应内容:br; sock_get($gurl); functio...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn