PHP的三種HTTP請求
阿新 • • 發佈:2019-01-22
);
//構造要post的字串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$post .= "&";
$flag = 1;
}
$post.= $key."="; $post.= urlencode($value);
$flag = 1;
}
$length = strlen($post);
//建立socket連線
$fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."--->".$errno);
//構造post請求的頭
$header = "POST /flandy/getpost.php HTTP/1.1\r\n";
$header .= "Host:127.0.0.1\r\n";
$header .= "Referer:/flandy/post.php\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".$length."\r\n";
$header .= "Connection: Close\r\n\r\n";
//新增post的字串
$header .= $ post."\r\n";
//傳送post的資料
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //去除請求包的頭只顯示頁面的返回資料
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
?>
getpost.php的內容如下
<?php
echo "this is the data posted";
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
結果輸出:
this is the data posted
//構造要post的字串
foreach ($argv as $key=>$value) {
if ($flag!=0) {
$post .= "&";
$flag = 1;
}
$post.= $key."="; $post.= urlencode($value);
$flag = 1;
}
$length = strlen($post);
//建立socket連線
$fp = fsockopen("localhost",81,$errno,$errstr,10) or exit($errstr."--->".$errno);
//構造post請求的頭
$header = "POST /flandy/getpost.php HTTP/1.1\r\n";
$header .= "Host:127.0.0.1\r\n";
$header .= "Referer:/flandy/post.php\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".$length."\r\n";
$header .= "Connection: Close\r\n\r\n";
//新增post的字串
$header .= $
//傳送post的資料
fputs($fp,$header);
$inheader = 1;
while (!feof($fp)) {
$line = fgets($fp,1024); //去除請求包的頭只顯示頁面的返回資料
if ($inheader && ($line == "\n" || $line == "\r\n")) {
$inheader = 0;
}
if ($inheader == 0) {
echo $line;
}
}
fclose($fp);
?>
getpost.php的內容如下
<?php
echo "this is the data posted";
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
結果輸出:
this is the data posted