Questions about submitting Forms (multidimensional array + files) data to the server using Curl

Source: Internet
Author: User
Problems with using curl to submit a form (multidimensional array + file) data to the server
I set up a test server locally, apache+php, and would like to use Curl to automatically submit form data to the remote server.

The remote server form has two data to submit:
1. Input file: request to upload pictures
2. CheckBox: There will be multiple buttons selected

Problem:
When the following program is run, the checkbox array is converted to a string, and the program error is as follows:
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 using http_build_query processing $post_data, then the color array can be correctly uploaded to the server, but the file will also be used as the general query parameters, so that upload failed.
2, if you do not use Http_build_query, the file can be uploaded correctly, but the server caught the value of the color array is "array" and prompted "array to string conversion" error.
3, I look at the Curl Handbook on the php.net, found a guy with my situation a bit similar, but he used an associative array, so can detours, similar
$post _data = Array ("Method" = $_post["method"],
"Mode" = $_post["mode"],


"Product[thumbnail]" "=" @{$_files["thumbnail" ["Tmp_name"]} ");

can be solved, but my case is indexed array, imitating his appearance after writing is still invalid.


Do you know how to solve the problem?

Share to:


------Solution--------------------
' Color ' = = Array (' Red ', ' green ', ' Blue '),
Writing
' Color ' = http_build_query (' Red ', ' green ', ' Blue '),
Can't you?

Written so much 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 '


);

A request to simulate a post via curl;
function Senddatabycurl ($url, $data =array ()) {
To escape a space
$url = Str_replace (', ' + ', $url);
$ch = Curl_init ();
Setting options, including URLs
curl_setopt ($ch, Curlopt_url, "$url");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, curlopt_timeout,3); Definition Timeout 3 seconds
Post data
curl_setopt ($ch, Curlopt_post, 1);
Add the post variable to the
curl_setopt ($ch, Curlopt_postfields, ($data)); The required array is handled with the Http_bulid_query () function, OK.

Execute and get the contents of the URL address
$output = curl_exec ($ch);
$errorCode = Curl_errno ($ch);
Releasing the curl handle
Curl_close ($ch);
if (0!== $errorCode) {
return false;
}
return $output;

}
$url = "http://localhost/test/test1.php";
Send an interface request via the post of Curl
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.