php中http协议是什么东西,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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值