如何通过cURL发布数组字段数据?

时间:2014-03-06 14:34:09

标签: php arrays post curl

我需要以以下形式提交POST数据:

first_name=Joe&last_name=Jones&fruit[]=Apple&fruit[]=Orange&fruit[]=Banana

但是,使用下面的代码,我将数据发布为:

first_name=Joe&last_name=Jones&fruit[]=Array

如何更改cURL脚本以提交数据,如上面的第一个示例所示?

以下是当前代码:

<?php
// Setup empty fields
$first_name = $last_name = $fruit = "";

// Session
session_start();
session_register('first_name');
session_register('last_name');
session_register('fruit');
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['fruit'] = $fruit; 

// Redirect valid form to process
if($valid)
    //set POST variables
    { $url = 'http://example.com/submit.php';
    $fields = array( //your parameters here
                            'first_name' => $first_name,
                            'last_name' => $last_name,
                            'fruit[]' => $fruit
                    );

    //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, http_build_query($fields));

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

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

    echo http_build_query($fields);

    //close connection
    curl_close($ch);

}
}
?>
<!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">

                    <p><label for="first_name">First Name</label><input type="text" id="first_name" size="20" maxlength="120" name="first_name" /></p>
                    <p><label for="last_name">Surname</label><input type="text" id="last_name" size="20" maxlength="120" name="last_name" /></p>
                    <p><label for="fruit">Pick some fruit</label>
                        <select id="fruit" name="fruit" multiple="multiple">
                            <option value="Apple">Apple</option>
                            <option value="Orange">Orange</option>
                            <option value="Pear">Pear</option>
                            <option value="Banana">Banana</option>
                        </select></p>

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

感谢您的帮助。

*编辑使用HTTP_BUILD_QUERY *

这是新代码:

<?php
// Setup empty fields
$first_name = $last_name = $fruit = "";

// Session
session_start();
session_register('first_name');
session_register('last_name');
session_register('fruit');
$_SESSION['first_name'] = $first_name;
$_SESSION['last_name'] = $last_name;
$_SESSION['fruit'] = $fruit; 

// Redirect valid form to process
if($valid)
    //set POST variables
    { $url = 'http://example.com/submit.php';
    $fields = array( //your parameters here
                            'first_name' => $first_name,
                            'last_name' => $last_name,
                            'fruit[]' => $fruit
                    );

    //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, http_build_query($fields));

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

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

    echo http_build_query($fields);

    //close connection
    curl_close($ch);

}
}
?>
<!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">

                    <p><label for="first_name">First Name</label><input type="text" id="first_name" size="20" maxlength="120" name="first_name" /></p>
                    <p><label for="last_name">Surname</label><input type="text" id="last_name" size="20" maxlength="120" name="last_name" /></p>
                    <p><label for="fruit">Pick some fruit</label>
                        <select id="fruit" name="fruit" multiple="multiple">
                            <option value="Apple">Apple</option>
                            <option value="Orange">Orange</option>
                            <option value="Pear">Pear</option>
                            <option value="Banana">Banana</option>
                        </select></p>

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

启用打印选项后,返回的字符串采用以下格式:

first_name=Joe&last_name=Jones&fruit%5B%5D=

所以有两个问题:

1)未提交水果阵列 2)需要保留方括号格式,但替换为%5B%5D

我需要的格式是:

    first_name=Joe&last_name=Jones&fruit[]=Apple&fruit[]=Orange

关于我的错误的任何想法?感谢

2 个答案:

答案 0 :(得分:1)

使用http_build_query()

$readyToPost = http_build_query($_SESSION);

curl_setopt($ch, CURLOPT_POSTFIELDS, $readyToPost);

答案 1 :(得分:0)

您正在对foreach循环内的数组进行连接。

你的阵列:

'fruit' => array($fruit)

在进行如下所述的连接时,您会在Array内以$value作为纯文字获取:

$fields_string .= $key.'='.$value.'&';

所以最终你要张贴:first_name=Joe&last_name=Jones&fruit=Array

我的建议是使用http_build_query()并删除您手动发布的数据。

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