我想用curl发送复杂的post数据。
我试图发送的数据:
Array
(
[test] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[file] => CURLFile Object
(
[name] => H:\wwwroot\curl/upload.txt
[mime] =>
[postname] =>
)
)
我需要使用post端的变量作为$_post[“test”]和$_files[“file”]
但我不知道。对于(有时是多维)数组数据,我需要http_build_查询,但这会破坏文件。如果我不使用http-build-query,我的数组会给出一个“数组到字符串转换”错误。
我怎样才能让它工作?
代码:
索引文件
$curl = curl_init();
$postValues = Array("test" => Array(1,2,3));
$postValues["file"] = new CurlFile(dirname(__FILE__). "/upload.txt");
curl_setopt($curl, CURLOPT_URL, "localhost/curl/post.php");
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postValues);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$curlResult = curl_exec($curl);
$curlStatus = curl_getinfo($curl);
echo $curlResult;
PHP
print_r($_REQUEST);
print_r($_FILES);
最佳答案
经过很长时间的研究来解决同样的问题,我认为一个更简单的解决方案是:$postValues = Array("test[0]" => 1, "test[1]" => 2, "test[2]" => 3);
这是模拟浏览器上发生的事情的正确方法
<input type="hidden" name="test[0]" value="1">
<input type="hidden" name="test[1]" value="2">
...
结果是:
Array
(
[test] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Array
(
[file] => Array
(
[name] => upload.txt
[type] => application/octet-stream
[tmp_name] => /tmp/phprRGsPU
[error] => 0
[size] => 30
)
)