MCG
MCG

Reputation: 124

How can I post array field data via cURL?

I need to submit POST data in the form:

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

However, using the code below, I get data posted as:

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

How can I change the cURL script to submit data as shown in the first example above?

Here is the current code:

<?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>

Thanks for your help.

* EDIT TO USE HTTP_BUILD_QUERY *

This is the new code:

<?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>

With the print option enabled, the string returned is in the format below:

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

So 2 issues:

1) The fruit array is not submitted 2) The square bracket format needs to be retained, but is replaced with %5B%5D

The format I need is:

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

Any ideas as to what I have wrong? Thanks

Upvotes: 3

Views: 4475

Answers (2)

Sabuj Hassan
Sabuj Hassan

Reputation: 39365

You are doing concatenation over the array inside the foreach loop.

Your Array:

'fruit' => array($fruit)

And when doing a concatenation like below, you are getting Array as plain text inside the $value:

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

So ultimately you are posting: first_name=Joe&last_name=Jones&fruit=Array

My suggestion is to use http_build_query() and remove your manual post data building.

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

Upvotes: 0

Pavel Štěrba
Pavel Štěrba

Reputation: 2912

Use http_build_query():

$readyToPost = http_build_query($_SESSION);

curl_setopt($ch, CURLOPT_POSTFIELDS, $readyToPost);

Upvotes: 1

Related Questions