Using curl to submit data from a form (multi-dimensional array + file) 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 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?
[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]

 'Red', 'B [2]' => 'red', 'C [3] '=> 'red', 'e [1]' => "@ d: \ img.gif ", 'e [2] '=>" @ d: \ 2.gif"); // simulate post requests through curl; 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 )); // use the http_bustm_query () function to process the array to be uploaded. then, it is OK. // 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 echo SendDataByCurl ($ url, $ post_data) through the curl post method;?>

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.