我尝试通过发送一个多数组,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
或者[]
我总是在没有数据的数组中得到和(字符串)数组。
任何人都可以帮我做吗?
用于更快测试的整个代码:
$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;
测试.php
print_r($_POST);
谢谢你的帮助!
干杯