(PHP) $_POST +卷曲+多阵列
我尝试通过 cURL
发送多数组,但找不到好的方法。
我的代码示例:
$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
这将起作用并在 curl_init()
站点上给出我想要的结果:
print_r( $_POST )
:
Array (
[a] => testa
[b] => testb
[c] => Array (
[d] => test1
[e] => test2
)
)
我想动态添加 c 数组例如:
$c['d'] = 'test1';
$c['e'] = 'test2';
但是,如果我尝试使用 array_push
或 []
添加数组,我总是会在数组中得到 and (string)Array ,而没有数据。
有人可以帮我做吗?
用于更快测试的整个代码:
$url = 'url_to_test.php';
$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$buffer = curl_exec ($ch);
curl_close ($ch);
echo $buffer;
test.php
print_r($_POST);
感谢您的帮助!
干杯
I try to send a multi array via cURL
but I can't find a nice way to do it.
My code example:
$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
This will work and give the result I want on the curl_init()
site:
print_r( $_POST )
:
Array (
[a] => testa
[b] => testb
[c] => Array (
[d] => test1
[e] => test2
)
)
I'd like to add the c array dynamically like:
$c['d'] = 'test1';
$c['e'] = 'test2';
But if I try to add an array with array_push
or []
I always get and (string)Array in the Array without data.
Can anyone help me to do it?
The whole code for faster testing:
$url = 'url_to_test.php';
$data = array( 'a' => 'testa', 'b' => 'testb', 'c[d]' => 'test1', 'c[e]' => 'test2' );
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$buffer = curl_exec ($ch);
curl_close ($ch);
echo $buffer;
The test.php
print_r($_POST);
Thanks for any help!
cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需
添加新的字符串键“c[d]”和“c[e]”。
如果您想要一个嵌套数组,请使用:
-- 编辑 --
您正在尝试设置 POST 数据,它本质上是一组键值对。您无法提供嵌套数组。但是,您可以序列化嵌套数组并在另一端对其进行解码。例如:
在接收端:
In
You've simply added new string keys "c[d]" and "c[e]".
If you want a nested array, use:
-- EDIT --
You're trying to set POST data, which is essentially a set of key value pairs. You can't provide a nested array. You could, however, serialize the nested array and decode it at the other end. Eg:
And at the receiving end:
这是您的解决方案
service.php 代码
here is your solution
service.php code