使用curl 提交表单(多维数组+文件)数据到服务器的问题
我在本地搭了一个测试服务器,Apache+PHP,想使用curl自动提交表单数据到远程服务器。
远程服务器表单有两项数据需要提交:
1、input file: 要求传图片
2、checkbox: 会有多个按钮被选中
问题:
运行时下面程序时checkbox数组会被转成字符串,程序报错如下:
Array to string conversion
主要代码如下:
$post_url = "/post.php";
$post_data = array(
'color' => array('red', 'green', 'blue'),
'img' => "@d:\image.jpg"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($post_data));
if (false === curl_exec($ch)) {
echo "Curl_error: " . curl_error($ch);
}
curl_close($ch);
尝试过:
1、如果用http_build_query处理$post_data,那么color数组就可以正确的传到服务器,但是文件也会被当成一般query参数,从而上传失败。
2、如果不使用http_build_query,文件可以正确上传,但是在服务器抓到color数组的值就是"Array",并提示"Array to string conversion"错误。
3、我在上看curl手册,发现有个家伙跟我的情况有点类似,但是他使用的是关联数组,所以可以绕弯,类似
$post_data = array("method" => $_POST["method"],
"mode" => $_POST["mode"],
"product[name]" => $_POST["product"],
"product[cost]" => $_POST["product"]["cost"],
"product[thumbnail]" => "@{$_FILES["thumbnail"]["tmp_name"]}");
即可解决,可是我的情况是索引数组,模仿他的样子写了之后仍然无效。
请教各位朋友是否知道如何解决?