3

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

1
  • 1
    Don't build the query string manually. Use http_build_query() instead. Commented Mar 6, 2014 at 14:38

2 Answers 2

1

Use http_build_query():

$readyToPost = http_build_query($_SESSION);

curl_setopt($ch, CURLOPT_POSTFIELDS, $readyToPost);
Sign up to request clarification or add additional context in comments.

3 Comments

Does that replace the "//url-ify the data for the POST" lines? I'm trying and failing to add http_build_query in the correct location. Thank you.
Yes, it will return you string which can be directly passed to curl_setopt, see my updated post.
Thank you. That posts, but printing the data shows first_name=Joe&last_name=Jones&fruit= <- The array data doesn't come through. Also, I need to retain the square brackets and they are not showing. Thanks again.
0

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));

7 Comments

Thanks Sabuj.How can I add in a print of the query generated, so I can check it is now in the correct format? Thanks
echo http_build_query($fields); will do for you. If you want to get the print from your server side(if you are posting to your server) then do a print_r($_POST);
Hi Sabuj. I tried adding the echo and that showed the code, thanks. However, I still have an issue with the data submitted. I have updated my original question to show the new code that uses http_build_query. Please can you tell me where I'm going wrong? Thank you
@MCG probably your fruit is empty. Try this $fruit = array('mango', 'banana');
Thanks @Sabuj. How would I populate the curl array from a multi-select field where people select one or more fruits? My code in the original question above doesn't achieve this. Thank you.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.