为什么我的cURL POST数据没有填充字段?

时间:2014-03-04 17:47:18

标签: php post curl

我搜索过以前的帖子,但使用类似的代码方法找不到相同问题的示例。

我在将用户发送到“感谢”确认页面之前,通过cURL将数据提交到远程网址。

当我提交表单(下面的完整代码)时,不会填充远程URL字段,并且每个表单字段都保持为空:

<?php
// Setup empty fields
$SubmitForm_first_name = $SubmitForm_last_name = $SubmitForm_job_title = "";
$SubmitForm_first_nameError = $SubmitForm_last_nameError = $SubmitForm_job_titleError = "";

// Validate Field Entry
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $valid = true; // Redirect if valid

    if (empty($_POST["SubmitForm_first_name"]))
    {$valid = false; $SubmitForm_first_nameError = "First name required";}
        else
        {$SubmitForm_first_name = htmlspecialchars($_POST["SubmitForm_first_name"]);}

    if (empty($_POST["SubmitForm_last_name"]))
    {$valid = false; $SubmitForm_last_nameError = "Surname required";}
        else
        {$SubmitForm_last_name = htmlspecialchars($_POST["SubmitForm_last_name"]);}

    if (empty($_POST["SubmitForm_job_title"]))
    {$valid = false; $SubmitForm_job_titleError = "Job title required";}
        else
        {$SubmitForm_job_title = htmlspecialchars($_POST["SubmitForm_job_title"]);}

// Start session
session_start();

// Register
session_register('SubmitForm_first_name');
session_register('SubmitForm_last_name');
session_register('SubmitForm_job_title');

// Populate
$_SESSION['SubmitForm_first_name'] = $SubmitForm_first_name;
$_SESSION['SubmitForm_last_name'] = $SubmitForm_last_name;
$_SESSION['SubmitForm_job_title'] = $SubmitForm_job_title; 

// Redirect valid form to process
if($valid)
    //set POST variables
    { $url = 'http://www.example.com/submit.php';
    $fields = array(
                        'SubmitForm[first_name]' => urlencode($SubmitForm_first_name),
                        'SubmitForm[last_name]' => urlencode($SubmitForm_last_name),
                        'SubmitForm[job_title]' => urlencode($SubmitForm_job_title)
                    );

    //url-ify the data for the POST
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    rtrim($fields_string, '&');

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);

    // redirect to thank you page
    header('Location: http://www.example.com/thanks.php');
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
        <div>

        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="submit_data" method="POST" id="submit_data">

            <fieldset>
                <legend>Your Details</legend>
                    <p><label for="SubmitForm_first_name">First Name</label><input type="text" id="SubmitForm_first_name" size="20" maxlength="50" value="<?php echo $SubmitForm_first_name;?>" name="SubmitForm_first_name" /><br /><span class="error"> <?php echo $SubmitForm_first_nameError;?></span></p>
                    <p><label for="SubmitForm_last_name">Surname</label><input type="text" id="SubmitForm_last_name" size="20" maxlength="50" value="<?php echo $SubmitForm_last_name;?>" name="SubmitForm_last_name" /><br /><span class="error"> <?php echo $SubmitForm_last_nameError;?></span></p>
                    <p><label for="SubmitForm_job_title">Job Title</label><input type="text" id="SubmitForm_job_title" size="30" maxlength="30" value="<?php echo $SubmitForm_job_title;?>" name="SubmitForm_job_title" /><br /><span class="error"> <?php echo $SubmitForm_job_titleError;?></span></p>
            </fieldset>

            <input type="submit" value="Submit" />
        </form>
        </div>
    </body>
</html>

但是,如果我执行直接HTTP发布(下面的代码示例),则每个字段都会正确填充:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
        <div>

    <form action="http://www.example.com/submit.php" name="inputData" method="POST" id="inputData">

            <fieldset>
                <legend>Your Details</legend>
                    <p><label for="SubmitForm_first_name">First Name</label><input type="text" id="SubmitForm[first_name]" size="20" maxlength="50" value="<?php echo $SubmitForm_first_name;?>" name="SubmitForm[first_name]" /><br /><span class="error"> <?php echo $SubmitForm_first_nameError;?></span></p>
                    <p><label for="SubmitForm_last_name">Surname</label><input type="text" id="SubmitForm[last_name]" size="20" maxlength="50" value="<?php echo $SubmitForm_last_name;?>" name="SubmitForm[last_name]" /><br /><span class="error"> <?php echo $SubmitForm_last_nameError;?></span></p>
                    <p><label for="SubmitForm_job_title">Job Title</label><input type="text" id="SubmitForm[job_title]" size="30" maxlength="30" value="<?php echo $SubmitForm_job_title;?>" name="SubmitForm[job_title]" /><br /><span class="error"> <?php echo $SubmitForm_job_titleError;?></span></p>
            </fieldset>

            <input type="submit" value="Submit" />
        </form>
        </div>
    </body>
</html>

第三方网址需要以下字段:

“SubmitForm [first_name]”,“SubmitForm [last_name]”和“SubmitForm [job_title]”

请任何人都能发现为什么cURL示例会导致远程表单上的空字段?

非常感谢


更新

<?php
// Setup empty fields
$SubmitForm_first_name = $SubmitForm_last_name = $SubmitForm_job_title = "";
$SubmitForm_first_nameError = $SubmitForm_last_nameError = $SubmitForm_job_titleError = "";

// Validate Field Entry
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    $valid = true; // Redirect if valid

    if (empty($_POST["SubmitForm_first_name"]))
    {$valid = false; $SubmitForm_first_nameError = "First name required";}
        else
        {$SubmitForm_first_name = htmlspecialchars($_POST["SubmitForm_first_name"]);}

    if (empty($_POST["SubmitForm_last_name"]))
    {$valid = false; $SubmitForm_last_nameError = "Surname required";}
        else
        {$SubmitForm_last_name = htmlspecialchars($_POST["SubmitForm_last_name"]);}

    if (empty($_POST["SubmitForm_job_title"]))
    {$valid = false; $SubmitForm_job_titleError = "Job title required";}
        else
        {$SubmitForm_job_title = htmlspecialchars($_POST["SubmitForm_job_title"]);}

// Start session
session_start();

// Register
session_register('SubmitForm_first_name');
session_register('SubmitForm_last_name');
session_register('SubmitForm_job_title');

// Populate
$_SESSION['SubmitForm_first_name'] = $SubmitForm_first_name;
$_SESSION['SubmitForm_last_name'] = $SubmitForm_last_name;
$_SESSION['SubmitForm_job_title'] = $SubmitForm_job_title; 

// Redirect valid form to process
if($valid)
    //set POST variables
    { $url = 'http://www.example.com/submit.php';
    $fields = array(
                        'SubmitForm[first_name]' => $SubmitForm_first_name,
                        'SubmitForm[last_name]' => $SubmitForm_last_name,
                        'SubmitForm[job_title]' => $SubmitForm_job_title
                    );

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);

    // redirect to thank you page
    header('Location: http://www.example.com/thanks.php');
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    </head>
    <body>
        <div>

        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" name="submit_data" method="POST" id="submit_data">

            <fieldset>
                <legend>Your Details</legend>
                    <p><label for="SubmitForm_first_name">First Name</label><input type="text" id="SubmitForm_first_name" size="20" maxlength="50" value="<?php echo $SubmitForm_first_name;?>" name="SubmitForm_first_name" /><br /><span class="error"> <?php echo $SubmitForm_first_nameError;?></span></p>
                    <p><label for="SubmitForm_last_name">Surname</label><input type="text" id="SubmitForm_last_name" size="20" maxlength="50" value="<?php echo $SubmitForm_last_name;?>" name="SubmitForm_last_name" /><br /><span class="error"> <?php echo $SubmitForm_last_nameError;?></span></p>
                    <p><label for="SubmitForm_job_title">Job Title</label><input type="text" id="SubmitForm_job_title" size="30" maxlength="30" value="<?php echo $SubmitForm_job_title;?>" name="SubmitForm_job_title" /><br /><span class="error"> <?php echo $SubmitForm_job_titleError;?></span></p>
            </fieldset>

            <input type="submit" value="Submit" />
        </form>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:0)

当我运行您的代码时,我收到了通知Notice: Undefined variable: fields_string。我怀疑这与它有什么关系 - 脚本运行得很好。

出于某种原因,如果我打印出字符串,rtrim似乎没有切断最后&。这似乎对我没什么影响。再次,脚本运行得很好。

rtrim($fields_string, '&');
print $fields_string;

// OUTPUTS:
// SubmitForm[first_name]=XXXXX&SubmitForm[last_name]=YYYYY&SubmitForm[job_title]=Rock+Star&

脚本cURL调用只执行$ _POST的转储,一切都很顺利。

Array
(
    [SubmitForm] => Array
    (
        [first_name] => XXXXX
        [last_name] => YYYYY
        [job_title] => Rock Star
    )

)

所以也许尝试在目标脚本上转储$_POST,看看你是否得到同样的东西。如果没有,那么我怀疑这是cURL的错误。尝试将一些错误检查添加到您的cURL请求中。

$result = curl_exec($ch);

if(curl_errno($ch)) {
    print 'CURL Error: '.curl_error($ch);
}

看看是否会出现某种错误。常见问题是连接到https URL并且无法验证SSL证书。

答案 1 :(得分:0)

感谢@Quixrick和@Wrikken提供的非常有用的指示。

事实证明,第三方需要在其他字段中添加特定的“提交”字段。在发布的数据中添加它。