【问题标题】:PHP cURL post fields from user input来自用户输入的 PHP cURL 发布字段
【发布时间】:2022-01-06 08:02:17
【问题描述】:

我的问题是以下我有一个 HTML 网络表单

<!DOCTYPE html>
<html lang="en">
<head>
    <title>FORM</title>
<body>

<form action="/form/send/alex.php" method="post" >

Name: <input type="text" name="name" value="name"><br>
E-mail: <input type="text" name="email" value="email"><br>
<input type="submit">



</form>

</body>
</html>

我想使用 php curl 将用户输入摄取/发送到 Elasticsearch,但这不起作用:(而且我不知道如何从 Web 表单中执行此操作。如果我将 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // input fileds 更改为 @987654323 @ 正在工作,但是这个硬代码,我想插入用户输入。

现在这是后端 conde:

<?php

// THIS ARRAY CONTAINS THE INPUT FIELDS DATA
$data = array(
    "name" => "",
    "email" => ""
);




// START THE CURL PROCESS
$ch = curl_init(); // initialize


curl_setopt($ch, CURLOPT_URL, 'ip/index/_doc?pretty'); // form location url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); // form method
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // input fileds

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
?>

我收到的错误是 504 Gateway Time-out

感谢您的时间和帮助, 亚历克斯

【问题讨论】:

  • 你能用CURLOPT_URL中的完整网址试一次吗?
  • 我确实把完整的网址放在那里,正在工作,但只有硬编码输入:(
  • 您正在使用ip/index/_doc?pretty 这个网址,所以我说您可以尝试一次像http://example.com/ip/index/_doc?pretty 这样的完整网址。只需将example.com 替换为您的主机名,例如localhost 或其他名称。因为 CURL 不能自动检测所以你应该尝试一次。
  • 在您的“工作”版本中,您发送的是 JSON。 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); - 这里的 $data 不会自己神奇地变成 JSON。
  • @CBroe 的工作版本是这样的:&lt;?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'ip:port/remote/_doc?pretty'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "\n{\n \"@timestamp\": \"2021-11-29T08:21:15.000Z\",\n \"message\": \"monday\"\n}\n"); $headers = array(); $headers[] = 'Content-Type: application/json'; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo 'Error:' . curl_error($ch); } curl_close($ch);

标签: php forms elasticsearch curl


【解决方案1】:

如果您将使用curl_setopt($ch, CURLOPT_URL, 'ip/index/_doc?pretty');,那么它将抛出错误Error: Could not resolve host: ip,因为它假设主机名是ip/,因此您应该使用HOST/URL,如http://yourhost.com/ip/index/_doc?pretty。您可以查看我刚刚将 URL 替换为 google.com 的代码,它工作正常,因为现在可以访问主机。

<?php

// THIS ARRAY CONTAINS THE INPUT FIELDS DATA
$data = array(
    "name" => "",
    "email" => ""
);

// START THE CURL PROCESS
$ch = curl_init(); // initialize


curl_setopt($ch, CURLOPT_URL, 'http://google.com'); // form location url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1); // form method
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // input fileds

$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error: ' . curl_error($ch);
} else {
    echo 'Form submitted successfully';
}

curl_close($ch);

?>

【讨论】:

  • 嘿@Sateesh,谢谢回答,但curl_setopt($ch, CURLOPT_URL, 'ip/index/_doc?pretty'); 中的“ip”一词不是我的dns 名称,我只是用“ip”更改了真实主机,我不想要公开分享目的地。
  • 好的,知道了。如果解决方案有效,请喜欢。谢谢
猜你喜欢
  • 2015-05-31
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多