【发布时间】:2011-09-22 00:52:58
【问题描述】:
我尝试通过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);
感谢您的帮助!
干杯
【问题讨论】:
标签: php curl multidimensional-array http-post array-push