3

I came across a problem recently when I tried to pass multiple data's

The problem:

The API use POST to send parameters One of the parameter is designed to be able to accept multiple values with the same name/key. In GET it could be represented as such : /api?name=James&name=Peter&name=Richard&… In this example, we are sending 3 values for “name” (James, Peter and Richard), and this is perfectly valid since the API is expecting it and will process the value as it should. The problem now lies with POST. In PHP cURL, a POST parameter is sent like this:

 $data = array("name" => "James", "email" => "james@someone.com");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Now we all know in an array, we only set the key once. Thus there is no way to send 3 different value for “name” in this case. So, how are we suppose to send ?name=James&name=Peter&name=Richard by POST, but not by PHP this time but in HTTP curl format?

So how do I go sending multiple data in HTTP post something like this

 {
  "funcrr":"create_insert_new_delivery",
  "data":{
     "name":"James",
     "email":"james@someone.com",

//MORE OF LIKE AN ARRAY BUT IN HTTP FORM
       "product_name":"Pear",
       "product_weight":"30",
         "product_name":"Rice",
         "product_weight":"60",
          "product_name":"Yoghury",
           "product_weight":"100",
   }
}
aofdev
  • 1,772
  • 1
  • 15
  • 29
chiefo
  • 281
  • 1
  • 5
  • 15
  • repeating the request variables like that doesn't sound right at all, is the API something public you can share documentation on? Otherwise, you'd have to manually build your `CURLOPT_POSTFIELDS` string eg https://stackoverflow.com/a/5224895/61795 – Scuzzy Jul 04 '18 at 02:03
  • I am just guessing but have you tried sending `$data = array("name" => array("James", "Peter", "Richard"), "email" => "james@someone.com");` ? – Jay Bhatt Jul 04 '18 at 02:04
  • @jaybhatt yes, through http form please could u read the question in full – chiefo Jul 04 '18 at 02:13
  • @scuzzy yes, but i want to do it in HTTP form not PHP – chiefo Jul 04 '18 at 02:14
  • @chiefo so you want to create an HTML form and submit it directly to API without any PHP? – Jay Bhatt Jul 04 '18 at 02:16
  • 1
    @Scuzzy "repeating the request variables like that doesn't sound right." For a public example, see RFC 7578 for the multipart/form-data Content-Type (https://tools.ietf.org/html/rfc7578#page-5), you'll see it actually *requires* multiple file uploads to use the same name. See section 4.3: *To match widely deployed implementations, multiple files MUST be sent by supplying each file in a separate part but all with the same "name" parameter.* – Syntax Junkie Sep 15 '19 at 21:59
  • @RandallStewart Thanks for the link, My answer was able to help the question author. – Scuzzy Sep 15 '19 at 22:33

1 Answers1

2

This is the only way I can think of to build a request with duplicate named keys...

// Data Source
$data = array(
  array('name'=>'james','email'=>'james@someone.com'),
  array('name'=>'john','email'=>'john@someone.com'),
  array('name'=>'joe','email'=>'joe@someone.com'),
);

// Request String Building
$postFields = implode( '&', array_map( 'http_build_query', $data ) );

// curl assignment
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields );

This produces...

name=james&email=james%40someone.com&name=john&email=john%40someone.com&name=joe&email=joe%40someone.com

http_build_query() is applied to each "row" of data, which is then joined together with & via the implode()

Scuzzy
  • 12,186
  • 1
  • 46
  • 46