获取 php 个输入值

Get php input values

我的目标是拥有一个 WordPress 表单,在发布时将输入发送到 API 到 ERP。

现在按我的方式发送好像是空值

在 CURLOPT_POSTFIELDS 中,如果我输入的不是“$inputname”,而是“Martin”之类的东西,它就可以完美运行。

如何获取我要发送的4个字段的值?我尝试了 $POST['inputname'] 但同样,不起作用。

我根本不是 php 方面的专家,所以任何提示都将不胜感激!谢谢!

<?php       
            if($_POST['Envoyer']) {
      ?>
                <form id="formid2" action="" method="POST">                            
                        <input type="submit" name="Envoyer2" value="Recommencer" />
                </form>
                <?

                $curl = curl_init();

                curl_setopt_array($curl, array(
                CURLOPT_URL => "*******",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS =>"{\r\n    \"FirstName\": \"$inputname\",\r\n    \"Name\": \"$inputsurname\",\r\n    \"PhoneNo\": \"$inputphone\",\r\n    \"No\": \"$inputcomment\"\r\n}",
                CURLOPT_HTTPHEADER => array(
                    "Authorization: Basic ************",
                    "Content-Type: application/json",
                    "Cookie: RuntimeTenantAffinity=*******"
                ),
                ));
                
                $response = curl_exec($curl);

                curl_close($curl);
                echo $INPUTN;

            } else {
                ?>
                    <form id="formid" action="" method="POST">                            
                        <p>Nom : <input type="text" name="inputname" value="" /></p>
                        <p>Prénom : <input type="text" name="inputsurname" value="" /></p>
                        <p>Téléphone : <input type="text" name="inputphone" value="" /></p>
                        <p>Commentaire : <input type="text" name="inputcomment" value="" /></p>
                        <input type="submit" name="Envoyer" value="Envoyer" />
                    </form>
                <?
            }

            ?>

您不应手动创建 json,如果用户输入中存在某些特殊字符,则会产生错误。

创建一个数组,然后使用 json_encode 在您的 curl 中传递数据。同样对于 POST 请求,不要使用 CUSTOMREQUEST 选项,而是使用 CURLOPT_POST.

// User inputs are $inputname, $inputsurname, $inputphone, $inputcomment
// Create an array 
$arrData = [
    'FirstName' => $inputname,
    'Name'      => $inputsurname,
    'PhoneNo'   => $inputphone,
    'No'        => $inputcomment
];

// Now pass it in curl options array
[
    ...
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => json_encode($arrData),
]

如果api支持,您也可以传递数组中的数据。以下是您可以使用的选项。

CURLOPT_POSTFIELDS

The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.

最后强烈建议您对 curl 使用错误检查,这样您就可以知道出了什么问题。

if(curl_exec($curl) === false){
    echo 'Curl error: ' . curl_error($curl);
}
else{
    echo 'Operation completed without any errors';
}

感谢 Jitendra Yadav,我成功发送了数据!

如果有人想知道,我是使用 Jitendra 建议的数组来完成的,但我没有将变量发布为 $inputname,而是这样做的:

$arrData = [
                        'FirstName' => $_POST['inputname'],
                        'Name'      => $_POST['inputsurname'],
                        'PhoneNo'   => $_POST['inputphone'],
                        'No'        => $_POST['inputcomment']
                    ]; 

现在一切正常。再次感谢!