1. 程式人生 > >模擬get和post請求

模擬get和post請求

|------|get請求

$get.php

//php程式碼

<?php


//連結
$fp = fsockopen('localhost',80,$errno,$errstr, 10);

//判斷
if(!$fp){
echo $errstr;die;
}
//模擬

//1.請求行
$str = "GET /%e5%a4%8d%e4%b9%a0/%e6%a8%a1%e6%8b%9fget%20post%e8%af%b7%e6%b1%82/server.php HTTP/1.1\r\n";
//2.請求頭

//\r\n  換行  最後一個$str   兩個\r\n
$str .= "Host: localhost\r\n";
$str .= "Connection: close\r\n";
$str .= "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36\r\n";
$str .= "Accept-Language: zh-CN,zh;q=0.9\r\n";
$str .= "Cookie: age=20\r\n";

$str .= "Referer: http://localhost/%e5%a4%8d%e4%b9%a0/%e6%a8%a1%e6%8b%9fget%20post%e8%af%b7%e6%b1%82/get.html\r\n\r\n";

//3.請求體
// 寫入資訊
fwrite($fp, $str);

$res = '';
while(!feof($fp)){
$res .= fgets($fp);
}

echo $res;

fclose($fp);
?>

 

server.php

<?php
echo 123;

var_dump($_COOKIE['age']);
?>