【问题标题】:Sending the form data from my website to a remote http server via php通过 php 将表单数据从我的网站发送到远程 http 服务器
【发布时间】: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请求

我做错了什么?

【问题讨论】:

    标签: php html http-post


    【解决方案1】:

    我强烈建议使用 Guzzle,但 stream_context_create 的文档使用 fopen() 而不是 file_get_contents(),所以这可能是一个因素。

    另一个是标题。文档页面上的 cmets 以这种方式为 POST 请求设置标头:

    'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
                    . "Content-Length: " . strlen($body) . "\r\n",
    

    查看the answers here 以获得更多示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多