【发布时间】:2020-06-22 07:19:45
【问题描述】:
大家好
我从 html 表单接收到我的 action.php 文件的 http post 请求,然后这个 php 文件将这些值写入文本文件。 现在我希望 php 文件将其接收到的数据发送 到远程 http 服务器进行进一步处理(我使用的是简单的 python http 服务器)
这是我的 php 代码:
<?php
$data1 = $_REQUEST['key1'];
$data2 = $_REQUEST['key2'];
$data3 = $_REQUEST['key3'];
$data4 = $_REQUEST['key4'];
$data5 = $_REQUEST['key5'];
$fp = fopen('datafile.txt', 'w+');
fwrite($fp, implode("\n", [$data1, $data2, $data3, $data4, $data5]));
fclose($fp);
// example data
$data = array(
'key1'=> $data1,
'key2'=> $data2,
'key3'=> $data3,
'key4'=> $data4,
'key5'=> $data5
);
// build post body
$body = http_build_query($data); // foo=bar&baz=boom
// options, headers and body for the request
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Accept-language: en\r\n",
'data' => $body
)
);
// create request context
$context = stream_context_create($opts);
// do request
$response = file_get_contents('http://2.22.212.12:42221', false, $context)
?>
但是当我提交表单时,只生成了datafile.txt文件,并没有向远程python服务器发送post请求
我做错了什么?
【问题讨论】: