如何将数据从HTML表单传递到CURLOPT_POSTFIELDS?

时间:2018-01-23 20:40:26

标签: php

我正在尝试将简报订阅信息(名称和电子邮件)从HTML表单传递到CURLOPT_POSTFIELDS。

我的表单如下:

    <form class="form-signin" method="POST" action="URL_TO_POST_TO">
    <h2 class="form-signin-heading">Please sign in</h2>
    <input class="form-control" type="text" autofocus="" required="" placeholder="Email address">
    <input class="form-control" type="name" required="" placeholder="name">
    <label class="checkbox">

    </label>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
    </form>

我的PHP Curl看起来像:

<?php

$curl = curl_init();


$EMAIL = "Test";

curl_setopt_array($curl, array(
CURLOPT_URL => 
"https://spaces.nexudus.com/api/content/newslettersubscribers",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{\n\"BusinessId\": 469969507}", \n\"Name\",'. 
$EMAIL .',\n\\"Email\\": \\'.$EMAIL.'\\"\n"}',
CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer 
123345345345345345345345345345345",
    "Cache-Control: no-cache",
    "Content-Type: application/json",

),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

&GT?; 我在想可能有办法将表单传递给$ EMAIL变量。

对此的任何帮助将不胜感激。 谢谢,

1 个答案:

答案 0 :(得分:1)

您需要在表单中添加n操作:

<form class="form-signin" method="POST" action="path/to/file.php">

在输入中添加“name”属性:

<input name="email" class="form-control" type="text" autofocus="" required="" placeholder="Email address">

然后,提交后,进入服务器(PHP)端:

$EMAIL = $_POST['email'];