当前位置: 首页 > 后端技术 > PHP

PHP中的HTTP协议

时间:2023-03-30 01:50:43 PHP

1.HTTP协议是无状态的:每完成一次请求就结束连接,下一次请求与上一次请求无关。数据包:HTTP交互信息。Telnet模拟请求://GET模式,最后回车换行Aston$telnet127.0.0.180GET/Tools/Test/http.phpHTTP/1.1Host:localhost//POST模式,最后回车换行结束,输入参数Aston$telnet127.0.0.180POST/Tools/Test/http.phpHTTP/1.1Host:localhostContent-type:application/x-www-form-urlencodedContent-length:20name=chenjian&age=28fiddlerusage:usefile_get_contentto发送数据:$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);套接字模式:$data=array('name'=>'chenjian','age'=>28);$postData=http_build_query($data);$fp=fsockopen("本地主机",80,$errno,$errorStr,5);$request="POSThttp://localhost/http/socket.phpHTTP/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)){echofgets($fp,1024);}fclose($fp);curlexpansion:$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);

最新推荐
猜你喜欢