php模擬http請求需要實現以下步驟:
1.連線apache伺服器
使用fsockopen:專門用於連線伺服器,得到一個連線資源
2.寫入http協議
使用fwrite向資源寫入內容
3.接收資料
請求成功後返回的資料會被存放在資源中
4.解析資料:
使用fgets,和fgetc函式
實現程式碼:
<?php
//php模擬發出http請求
//1.連線目標伺服器apache
$f=fsockopen("localhost",98,$erron,$error);
//2.寫入http協議
//2.1拼湊http協議
//請求行
$http="GET /phpstudy/index.php HTTP/1.1\r\n";
//請求頭
$http .="Host:localhost\r\n";
//空行
$http .="\r\n";
//2.2寫給apache伺服器
if(fwrite($f,$http))
{
//寫入成功
//3.資料已經接收並存放在f資源中
//4.解析資源
//迴圈遍歷
while($line=fgets($f,1024))
//輸出
echo $line ."</br>";
}
php模擬http請求需要實現以下步驟:
1.連線apache伺服器
使用fsockopen:專門用於連線伺服器,得到一個連線資源
2.寫入http協議
使用fwrite向資源寫入內容
3.接收資料
請求成功後返回的資料會被存放在資源中
4.解析資料:
使用fgets,和fgetc函式
實現程式碼:
<?php
//php模擬發出http請求
//1.連線目標伺服器apache
$f=fsockopen("localhost",98,$erron,$error);
//2.寫入http協議
//2.1拼湊http協議
//請求行
$http="GET /phpstudy/index.php HTTP/1.1\r\n";
//請求頭
$http .="Host:localhost\r\n";
//空行
$http .="\r\n";
//2.2寫給apache伺服器
if(fwrite($f,$http))
{
//寫入成功
//3.資料已經接收並存放在f資源中
//4.解析資源
//迴圈遍歷
while($line=fgets($f,1024))
{
//輸出
echo $line ."</br>";
}
}