了解HTTP协议,模拟表单提交
一、file_get_content模拟表单填单提交
<?php
$postData = array(
'title' => '我是file_get_contents 构造器',
'content' => "我是file_get_contents构造的数据内容",
'publish' => "发布",
);
//数据转换格式:数组生成一个经过 URL-encode 的请求字符串。
$postData = http_build_query($postData);
$ops = array(
'http' => array(
'method' => 'POST',
'header' => 'Host:localhost\r\n' .
"Content-type:application/-form-urlencoded\r\n" .
"Content-length:" . strlen($postData) ."\r\n",
'content' => $postData,
)
);
//数据转换,写入文件(文件流)
$context = stream_context_create($osp);
// file_get_contents("http://localhost/http/post.php",false,$context);
$fp = fopen("http://localhost/http/post.php", 'r', false, $context);
fclose($fp);
二、curl模拟表单提交:
注意:此处需要开启curl拓展(php.ini文件)
$url ="";
$data = array();
//初始化一个curl会话‘
$ch = curl_init();
//设置相应的会话选项
//设置提交路径
curl_setopt($ch,CURLOPT_URL,$url);
//设置提交方式
curl_setopt($ch,CURLOPT_POST,1);
//设置数据提交
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
//数据提交成功后,把数据返回为字符串
curl_setopt($ch,CURLOPT_RETURNTRANSFER,$data);
//执行并获取结果
curl_multi_exec($ch);
//释放cURL句柄
curl_close($ch);
三、socket模拟表单提交