Posting multidimensional array with PHP and CURL

php curl post array
php curl post json
php multidimensional array
json array curl post
php api post curl
php multidimensional array with keys
post raw data curl php
php curl post raw json

I'm having trouble posting form data via CURL to a receiving PHP script located on a different host.

I get an Array to string conversion error

This is print_r of the array I'm posting:

Array
(
    [name] => Array
    (
        [0] => Jason
        [1] => Mary
        [2] => Lucy
    )
    [id] => 12
    [status] => local
    [file] => @/test.txt
)

This is the line the error occurs on:

curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post);

The third argument must be an array because I need the Content-Type header to be set to multipart/form-data as I am sending a file via this same array, therefore I cannot convert the array to a query string or use http_build_query().

Also I do not have access to the code on the receiving host so I cannot serialize and unserialize the array.

I'm assuming that the value of the name key being an array is the cause for this error, I'm also assuming that CURLOPT_POSTFIELDS doesn't support multidimensional arrays. Is there any other way around this or am I doomed?

Thanks in advance!

You'd have to build the POST string manually, rather than passing the entire array in. You can then override curl's auto-chose content header with:

curl_setopt($c, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));

Serializing/json-ifying would be easier, but as you say, you have no control over the receiving end, so you've got a bit of extra work to do.

Post multidimensional array using CURL and get the result on , cURL can only accept a simple key-value paired array where the values are strings, it can't take an array like yours which is an array of objects. The concept of an array doesn’t really exist when it comes to HTTP requests. PHP (and likely other server-side languages) has logic baked in that can take request data that looks like an array (to it) and puts it together as an array while populating $_GET, $_POST etc.

function http_build_query_for_curl( $arrays, &$new = array(), $prefix = null ) {

    if ( is_object( $arrays ) ) {
        $arrays = get_object_vars( $arrays );
    }

    foreach ( $arrays AS $key => $value ) {
        $k = isset( $prefix ) ? $prefix . '[' . $key . ']' : $key;
        if ( is_array( $value ) OR is_object( $value )  ) {
            http_build_query_for_curl( $value, $new, $k );
        } else {
            $new[$k] = $value;
        }
    }
}

$arrays = array(
    'name' => array(
        'first' => array(
            'Natali', 'Yura'
        )
    )
);


http_build_query_for_curl( $arrays, $post );

print_r($post);

How to POST multidimensional array data, curl-and-php. How to POST multidimensional array data I have to post and a site which is using data like multidimensional arrays like. Post multi-dimensional array using PHP Curl. . GitHub Gist: instantly share code, notes, and snippets.

The concept of an array doesn't really exist when it comes to HTTP requests. PHP (and likely other server-side languages) has logic baked in that can take request data that looks like an array (to it) and puts it together as an array while populating $_GET, $_POST etc.

For instance, when you POST an array from a form, the form elements often look something like this:

<form ...>
  <input name="my_array[0]">
  <input name="my_array[1]">
  <input name="my_array[2]">
</form>

or even:

<form ...>
  <input name="my_array[]">
  <input name="my_array[]">
  <input name="my_array[]">
</form>

While PHP knows what to do with this data when it receives it (ie. build an array), to HTML and HTTP, you have three unrelated inputs that just happen to have similar (or the same, although this isn't technically valid HTML) names.

To do the reverse for your cURL request, you need to decompose your array into string representations of the keys. So with your name array, you could do something like:

foreach ($post['name'] as $id => $name)
{
  $post['name[' . $id . ']'] = $name;
}
unset($post['name']);

Which would result in your $post array looking like:

Array
(
    [name[0]] => Jason
    [name[1]] => Mary
    [name[2]] => Lucy
    [id] => 12
    [status] => local
    [file] => @/test.txt
)

And then each key in the array you are posting would be a scalar value, which cURL is expecting, and the array would be represented as you need to for HTTP.

Posting multidimensional array with PHP and CURL, I'm having trouble posting form data via CURL to a receiving PHP script located on a different host. I get an Array to string conversion error. Find answers to Help posting a multidimensional array via CURL in PHP from the expert community at Experts Exchange

Simplest solution is to do a :

$array = urldecode(http_build_query($array));

Below is sample code where this is used in real life :

https://gist.github.com/gayanhewa/142c48162f72e68a4a23

When you have nested $params section in the above gist it will parse it accordingly and prepare it for posting via curl.

how to send multidimensional array post data in php , function checkCallback(){ $ch = curl_init("remote.domain.com/callback.php"); $​jsonData = [ "success" =>true, "error" => null, php curl multidimensional-array. curl-and-php . How to POST multidimensional array data. This message: I have to post and a site which is using data like multidimensional arrays

First I would like to thank Daniel Vandersluis for his insightful reply. Based on his input I came up with this to fix the problem from the original question:

<?php

function curl_postfields_flatten($data, $prefix = '') {
  if (!is_array($data)) {
    return $data; // in case someone sends an url-encoded string by mistake
  }

  $output = array();
  foreach($data as $key => $value) {
    $final_key = $prefix ? "{$prefix}[{$key}]" : $key;
    if (is_array($value)) {
      // @todo: handle name collision here if needed
      $output += curl_postfields_flatten($value, $final_key);
    }
    else {
      $output[$final_key] = $value;
    }
  }
  return $output;
}

Usage should look like this:

curl_setopt($this->ch, CURLOPT_POSTFIELDS, curl_postfields_flatten($post));

This function will convert arrays like this:

array(
  'a' => 'a',
  'b' => array(
    'c' => array(
      'd' => 'd',
      'e' => array(
        'f' => 'f',
      ),
    ),
  ),
);

Into this:

array(
  'a' => 'a',
  'b[c][d]' => 'd',
  'b[c][e][f]' => 'f',
)

It doesn't handle cases with mixed format when there is a key collision like this:

array(
 'b[c]' => '1',
 'b' => array(
   'c' => '2', 
  ),
);

The output will contain only the first value for that key

array(
 'b[c]' => '1'
)

Post multi-dimensional array using PHP Curl. · GitHub, Post multi-dimensional array to a URL using PHP Curl. * Usage: * $req = new MultiDimArrayPost();. * $req->postToURL('http://xyz.com/post',$_POST);. This makes my array multidimensional, when I use http_build_query to post this data the product gets created but the nested array is ignored because cURL does not handle these types of arrays to my knowledge unless you serialize them, but for that to work the api would have to unserialize the data received.

Help posting a multidimensional array via CURL in PHP Solutions , I've written a plugin for vBulletin that POSTs the necessary info via CURL during the login process: $url = 'http://example.com/form.php?form=7'; $fields = array(  I'm trying to post an array with cURL. I don't have problem to send a simple array, but i have when arrays include the file. I would like to send some parameters and one pdf in multidimensional array, but not send the file, just the filename and the other paramters.

Multidimensional Array posted in PHP with cURL, This makes my array multidimensional, when I use http_build_query to post this data the product gets created but the nested array is ignored  Accessing multidimensional array elements: There are mainly two ways to access multidimensional array elements in PHP. Elements can be accessed using dimensions as array_name[‘first dimension’][‘second dimension’]. Elements can be accessed using for loop. Elements can be accessed using for each loop.

Posting multidimensional array with PHP and CURL, I'm having trouble posting form data via CURL to a receiving PHP script located on a different host. I get an Array to string conversion error This is print_r of the  If you wish to pass PHP POST array variables without submitting a form, using the PHP Curl library to another URL, you first need to have a an Array which holds all the values for the POST array Once we have the array set, we create the variables for the destination URL and POST fields

Comments
  • Thanks! I didn't actually know I could do this. I added CURLOPT_HTTPHEADER and passed my array into http_build_query(). Job done!
  • Thanks for mentioning http_build_query(). It worked like a charm for me!
  • In my case this doesn't work. The error is Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0. What worked for me was this post of Khristenko Yura
  • very helpful, thanks! You'll need that if you have nested arrays and need to upload files
  • I feel this is the better solution, since it handles file uploads easily too. Thanks!
  • If $post is not defined yet, you can simply add return $new; on the end of the function
  • I changed (on my code) it to return the $new array. Is there any reason why you use &$new (by reference) and return void instead of just returning $new?
  • Thanks for your answer, it didn't quite answer the question but it was very insightful!