【问题标题】:PHP not posting data via curlPHP不通过curl发布数据
【发布时间】:2013-03-20 15:14:46
【问题描述】:

我有一个表单,您可以选择日期范围(#StartDate - #EndDate 或 #AllDates),它将数据写入 .csv 文件并强制下载。无论出于何种原因,我都无法传递 #StartDate 和 #EndDate 值。它们是由 glDatePicker 字段生成的,但即使我输入硬编码值,它们也不会被传递。我运行 $('#StartDate').val();在控制台中,它返回日期,但是当我取消注释

var_dump($_POST);

它返回一个空数组。后端脚本正在使用:

`FuncitionNameHere(trim($_POST["AllDates"]), trim($_POST["StartDate"]), trim($_POST["EndDate"]))`

相关部分是

<form action="path/to/curl.php" method="post" id="Form6">
    <ul>
        <li>
            <label for="StartDate">Start Date</label>
            <input type="text" id="StartDate" name="StartDate" />
        </li>
        <li>    
            <label for="EndDate">End Date</label>
            <input type="text" id="EndDate" name="EndDate" />
            <input type="submit" id="Button6" value="Send" />
        </li>
        <li>
            <label for="AllDates"><input type="checkbox" name="AllDates" id="AllDates" value="YES"> All Dates</label>
        </li>
    </ul>
</form>

以及将数据发送到远程数据库的 curl 脚本

<?php
    session_start();
    $url = 'https://remoteurl.com';
    $StartDate = $_POST["StartDate"];
    $EndDate = $_POST["EndDate"];
    $AllDates = $_POST["AllDates"];
    $fields = array(
        'StartDate'=>urldecode($StartDate),
        'EndDate'=>urldecode($EndDate),
        'AllDates'=>urldecode($AllDates)
    );
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    $fields_string = rtrim($fields_string,'& ');
    //var_dump($_POST);
    $ch = curl_init($url);
    session_write_close();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    $directory='/home/XXX/public_html/XXX/';
    $filename = 'Report.csv';
    $handle = fopen($directory.$filename, 'w');
    fwrite($handle, $output);
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="Report.csv"');
    echo $output;
?>

发布#AllDates 但没有发布#StartDate 或#EndDate 值的任何明显原因?

【问题讨论】:

  • 你是如何提交表格的?
  • 抱歉,不确定您的意思?它是一个 submitHandler: function(form) { $('#prcs6').fadeIn('fast').delay(2000).fadeOut('fast'); $('#StartDate, #EndDate').delay(2000).val(""); form.submit();返回假; },
  • 通过 JS,这就是我要问的。顺便问一下,您是否在浏览器上检查了发送到 curl.php 的 HTTP 请求?检查是否发送了这些参数
  • 他们没有被发送,这是我的问题。不知道为什么。
  • 等等,你说他们没有被发送,但那不是真的。它们已发送,但它们是空的。那是因为这个代码:$('#StartDate, #EndDate').delay(2000).val(""); 你清空这些输入然后提交表单。

标签: php post curl


【解决方案1】:

没有必要像那样解析你的$_POST。只需这样做,它可能会解决您的问题:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $_POST ));

【讨论】:

  • 我替换了这一行 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);与上述。没有快乐。
【解决方案2】:

我会自己回答这个问题——尽管功劳归于@Uby。由于这个原因,Chrome 没有发布数据:

$('#StartDate, #EndDate').delay(2000).val("");

虽然它在其他浏览器中工作。所以我在提交后清除字段值的解决方案是这样做

setTimeout(function(){ // Delay for Chrome
    $('#StartDate, #EndDate').val(''); // blank the input
}, 100);

现在它会在您点击提交后瞬间清除字段,让 Chrome 有机会在清除字段之前序列化数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    • 1970-01-01
    • 2012-08-01
    相关资源
    最近更新 更多