有没有办法在下面的代码后发布包含多个值的字符串?请考虑第三个postfield $ multiple输出多个值 提前致谢
<?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);
答案 0 :(得分:4)
我们可以在上面的代码中包含您的答案吗?
<?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);
例如
答案 1 :(得分:3)
您可以使用http_build_query创建此查询字符串。
例如
<?php
$username = 'user1';
$password = 'strongpassword';
$values = array(
'username' => $username,
'password' => $password,
'multiple' => array(
'value1',
'value2',
'value3',
)
);
echo http_build_query($values);
输出:
username=user1&password=strongpassword&multiple%5B0%5D=value1&multiple%5B1%5D=value2&multiple%5B2%5D=value3
换句话说,多个参数应为multiple[]=value1&multiple[]=value2&multiple[]=value3
答案 2 :(得分:0)
要发送多个值,您必须用逗号分隔它们:
[...]&multiple=a,b,c
答案 3 :(得分:0)
Geia sou Lambros,
您可以通过重复键来发送多个值,例如:
...&multiple=hello&multiple=world&...