3

Is there a way to post a string with multiple values following the code below? please consider the third postfield $multiple to output more than one value Thanks in advance

<?php

$ch = curl_init(); //http post to another server

curl_setopt($ch, CURLOPT_URL,"http://www.example.com");

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS,"username=$username&password=$password&multiple=$ "); 

// receive server response

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

print_r($server_output);

curl_close ($ch);

4 Answers 4

6

Can we include your answer on the above code?

<?php

$ch = curl_init(); //http post to another server

curl_setopt($ch, CURLOPT_URL,"http://www.example.com");

curl_setopt($ch, CURLOPT_POST, 1);

  $username = 'user1';
  $password = 'strongpassword';

  $values = array(
    'username' => $username,
    'password' => $password,
    'multiple' => array(
        'value1',
        'value2',
        'value3',
    )
  );

$params = http_build_query($values);

curl_setopt($ch, CURLOPT_POSTFIELDS,$params); 

// receive server response

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$server_output = curl_exec ($ch);

print_r($server_output);

curl_close ($ch);

For example

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

Comments

3

You can make this query string using http_build_query.

For example

<?php
  $username = 'user1';
  $password = 'strongpassword';

  $values = array(
    'username' => $username,
    'password' => $password,
    'multiple' => array(
        'value1',
        'value2',
        'value3',
    )
);

echo http_build_query($values);

Output:

username=user1&password=strongpassword&multiple%5B0%5D=value1&multiple%5B1%5D=value2&multiple%5B2%5D=value3

In other words multiple parameters should be multiple[]=value1&multiple[]=value2&multiple[]=value3

1 Comment

Of course, do what you want :) Or you mean this?
1

Geia sou Lambros,

you can send multiple values with repeating the key, for example:

...&multiple=hello&multiple=world&...

Comments

0

For sending multiple values you have to separate them with commas:

[...]&multiple=a,b,c

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.