程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 利用fsocket模擬GET和POST請求

利用fsocket模擬GET和POST請求

編輯:關於PHP編程

[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>";  
}  
}  
 
 
 
//fsocket模擬post提交 
$purl = "http://localhost/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]
<?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); 
}    
?> 

以下為運行結果:


以下為Firebug的查看結果:

 
作者:Fanteathy

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved