http协议与php关系,PHP中的HTTP协议
無狀態:每次請求完成就結束連接,下一次請求與上次請求沒有關系。
報文:HTTP交互的信息。
telnet模擬請求: // GET方式,最后回車換行
Aston$ telnet 127.0.0.1 80
GET /Tools/Test/http.php HTTP/1.1
Host:localhost
// POST方式,最后回車換行,輸入參數
Aston$ telnet 127.0.0.1 80
POST /Tools/Test/http.php HTTP/1.1
Host:localhost
Content-type:application/x-www-form-urlencoded
Content-length:20
name=chenjian&age=28
fiddler用法:
利用file_get_content來發送數據: $data = array(
'name' => 'chenjian',
'age' => 28
);
$postData = http_build_query($data);
$opts = array(
'http' => array(
'host' => "localhost\r\n",
'method' => "POST",
'header' => "Content-type:application/x-www-form-urlencoded\r\n" . "Content-length:".strlen($postData)."\r\n",
'content' => $postData
);
);
$context = stream_context_create($opts);
file_get_contents("http://localhost/http/index.php", false, $context);
socket方式: $data = array(
'name' => 'chenjian',
'age' => 28
);
$postData = http_build_query($data);
$fp = fsockopen("localhost", 80, $errno, $errorStr, 5);
$request = "POST http://localhost/http/socket.php HTTP/1.1\r\n";
$request .= "Host:locahost\r\n";
$request .= "Content-type:application/x-www-form-urlencoded\r\n";
$request .= "Content-length:" . strlen($postData) . "\r\n";
$request .= $postData;
fwrite($fp, $request);
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
curl拓展: $url = "http://localhost/http/curl.php";
$data = array(
'name' => 'chenjian',
'age' => 28
);
// 1. 初始化curl會話
$ch = curl_init();
// 2. 設置
curl_setopt($ch, CURLOPT_URL, $url); //提交網址
curl_setopt($ch, CURLOPT_POST, 1); //提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //提交數據
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //提交成功后返回數據字符串
// 3. 執行
$out_put = curl_exec($ch);
// 4. 關閉會話
curl_close($ch);
var_dump($out_put);
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的http协议与php关系,PHP中的HTTP协议的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot热部署加持
- 下一篇: Android内存优化之内存泄漏