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" => "[email protected]");
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":"[email protected]",

//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",
   }
}
7
  • 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 stackoverflow.com/a/5224895/61795 Commented Jul 4, 2018 at 2:03
  • I am just guessing but have you tried sending $data = array("name" => array("James", "Peter", "Richard"), "email" => "[email protected]"); ? Commented Jul 4, 2018 at 2:04
  • @jaybhatt yes, through http form please could u read the question in full Commented Jul 4, 2018 at 2:13
  • @scuzzy yes, but i want to do it in HTTP form not PHP Commented Jul 4, 2018 at 2:14
  • 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 (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. Commented Sep 15, 2019 at 21:59

1 Answer 1

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'=>'[email protected]'),
  array('name'=>'john','email'=>'[email protected]'),
  array('name'=>'joe','email'=>'[email protected]'),
);

// 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()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.