Questions about using curl to submit form (multi-dimensional array + file) data to the server

Source: Internet
Author: User
Tags array to string
To submit data from a form (multi-dimensional array + file) to the server using curl, I set up a test server locally, Apache + PHP, and want to use curl to automatically submit form data to the remote server. The form on the remote server has two data items to submit: 1. input & nbsp; file: & nbsp; required to upload image 2. use curl to submit the form (multi-dimensional array + file) to the server.
I set up a test server locally, Apache + PHP, and want to use curl to automatically submit form data to the remote server.

The remote server form has two data items to submit:
1. input file: Image upload required
2. checkbox: multiple buttons are selected.

Problem:
When the following program is running, the checkbox array will be converted into a string. The program reports the following error:
Array to string conversion

The main code is as follows:
    $post_url = "http://domain.com/post.php";
    $post_data = array(
        'color' => array('red', 'green', 'blue'),
        'img' => "@d:\image.jpg"
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $post_url);
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_POSTFIELDS, ($post_data));
    if (false === curl_exec($ch)) {
        echo "Curl_error: " . curl_error($ch);
    }
    curl_close($ch);


Tried:
1. if you use http_build_query to process $ post_data, the color array can be correctly transmitted to the server, but the file will also be treated as a general query parameter, thus uploading failure.
2. if http_build_query is not used, the file can be uploaded correctly, but the value of the color Array captured on the server is "Array", and the error "Array to string conversion" is returned.
3. I read the curl manual on php.net and found that a guy is similar to me, but he uses an associated array, so he can bend around.
$post_data = array("method" => $_POST["method"],
                    "mode" => $_POST["mode"],
                    "product[name]" => $_POST["product"], 
                    "product[cost]" => $_POST["product"]["cost"], 
                    "product[thumbnail]" => "@{$_FILES["thumbnail"]["tmp_name"]}");

I can solve this problem, but my problem is that the index array is still invalid after it is written in imitation of him.


Do you know how to solve this problem? Share:
------ Solution --------------------
'Color' => array ('red', 'green', 'blue '),
Writing
'Color' => http_build_query (array ('red', 'green', 'Blue ')),
No?

Writing is better.
'Color [0] '=> 'red ',
'Color [1] '=> 'green ',
'Color [2] '=> 'blue ',

------ Solution --------------------
 
$ Post_url = "http: // localhost/test/test1.php ";
$ Post_data = array (
'A [1] '=> 'red ',
'B [2]' => 'red ',
'C [3] '=> 'red ',

'E [1] '=> "@ d: \ img.gif ",
'E [2] '=> "@ d: \ 2.gif"


);

// Use curl to simulate post requests;
Function SendDataByCurl ($ url, $ data = array ()){
// Escape spaces
$ Url = str_replace ('', '+', $ url );
$ Ch = curl_init ();
// Set options, including URL
Curl_setopt ($ ch, CURLOPT_URL, "$ url ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 3); // defines a timeout value of 3 seconds.
// POST data
Curl_setopt ($ ch, CURLOPT_POST, 1 );
// Add the post variable
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, ($ data); // you can use the http_bu1__query () function to process the expected array.

// Execute and obtain the url content
$ Output = curl_exec ($ ch );
$ ErrorCode = curl_errno ($ ch );
// Release the curl handle
Curl_close ($ ch );
If (0! ==$ ErrorCode ){
Return false;
}
Return $ output;

}
$ Url = "http: // localhost/test/test1.php ";
// Send the interface request through the curl post method
Echo SendDataByCurl ($ url, $ post_data );

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.