[英]How to submit custom POST form via cURL PHP in wordpress
我正在制作一个主题 wordpress 在 localhost 中使用 childtheme。 我在 footer.php 中有一个基本形式footer.php
:
<form method="post">
<input name="FNAME" type="text">
<input name="LNAME" type="text">
<input name="EMAIL" type="email">
<button type="submit">Subscribe</button>
</form>
现在,我想通过 cURL ZE1BFD762321E409CEEZ4AC0B6 将表格提交给 api url 我知道像这样设置 cURL 的代码:
$data = $_POST;
$curl = curl_init();
$options =array(
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_URL => 'link_to_url',
CURLOPT_POST => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => http_build_query($data)
);
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
curl_close($curl);
我想在提交表单时,POST数据将通过这个cURL发送,但我不知道我应该把这个代码放在哪里以及如何处理它。 我想它可能放在 function.php 中的function.php
中,但我不知道怎么写。请帮帮我!
如果您希望在同一个文件中执行上述代码,可以执行以下操作:
<?php
// any other php code above
if(isset($_POST["infoForm"])) {
$curl = curl_init();
$data = array(
'post_request_name_for_fName' => $_POST["FNAME"],
'post_request_name_for_lName' => $_POST["FNAME"],
'post_request_name_for_email' => $_POST["EMAIL"]
);
$options =array(
CURLOPT_RETURNTRANSFER =>true,
CURLOPT_URL => 'link_to_url',
CURLOPT_POST => true,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POSTFIELDS => http_build_query($data)
);
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
curl_close($curl);
}
// any other php code below
?>
并将 HTML 设置为此:
<form method="post">
<input name="FNAME" type="text">
<input name="LNAME" type="text">
<input name="EMAIL" type="email">
<button type="submit" name="infoForm">Subscribe</button>
</form>
现在这里是它背后的解释。 您将提交按钮的name
设置为infoForm
(或您想要的任何其他名称),以便 PHP 可以知道发布哪个表单以及何时发布。 然后使用if(isset($_POST["infoForm"]))
检查表单是否已发布,然后执行较低的代码以发布它。
$_POST["FNAME"]
和其他变体检查在每个输入中找到的数据并将其设置为$data
数组的值。 然后,您可以将post_request_name_for_fName
的所有值更改为您的发布请求所需的任何值(即;如果您的发布请求需要“名称”和“电子邮件”参数,只需根据需要更改上述值)。
如果您还想转换到不同的页面,您可以创建一个新文件(即 post.php)并在没有 if(isset) 的情况下添加代码,并将 html 设置为:
<form method="post" action="post.php">
<input name="FNAME" type="text">
<input name="LNAME" type="text">
<input name="EMAIL" type="email">
<button type="submit" name="infoForm">Subscribe</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.