使用curl 提交表单(多维数组+文件)数据到服务器的问题 string http_build_query ( array formdata [, string numeric_prefix] );有关下标的要加前缀,见PHP手册 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 'color' => array('red', 'green', 'blue'),写作 'color' => http_build_query(array('red', 'green', 'blue')),不行吗?写成这样更好 'color[0]' => 'red', 'color[1]' => 'green', 'color[2]' => 'blue', <?php$post_url = "http://localhost/test/test1.php"; $post_data = array( 'a[1]' => 'red', 'b[2]' => 'red', 'c[3]' => 'red', 'e[1]' => "@d:\img.gif", 'e[2]' => "@d:\\2.gif" ); //通过curl模拟post的请求;function SendDataByCurl($url,$data=array()){ //对空格进行转义 $url = str_replace(' ','+',$url); $ch = curl_init(); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定义超时3秒钟 // POST数据 curl_setopt($ch, CURLOPT_POST, 1); // 把post的变量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, ($data)); //所需传的数组用http_bulid_query()函数处理一下,就ok了 //执行并获取url地址的内容 $output = curl_exec($ch); $errorCode = curl_errno($ch); //释放curl句柄 curl_close($ch); if(0 !== $errorCode) { return false; } return $output;}$url = "http://localhost/test/test1.php";//通过curl的post方式发送接口请求echo SendDataByCurl($url,$post_data);?> output:Array( [a] => Array ( [1] => red ) [b] => Array ( [2] => red ) [c] => Array ( [3] => red ))Array( [e] => Array ( [name] => Array ( [1] => img.gif [2] => 2.gif ) [type] => Array ( [1] => application/octet-stream [2] => application/octet-stream ) [tmp_name] => Array ( [1] => C:\Windows\Temp\php5C9E.tmp [2] => C:\Windows\Temp\php5C9F.tmp ) [error] => Array ( [1] => 0 [2] => 0 ) [size] => Array ( [1] => 43 [2] => 43 ) )) xuzuning版主:使用 'color[0]' => 'red', 'color[1]' => 'green', 'color[2]' => 'blue',这样的方式是可以的,感谢你的提醒,使我再次确认了一下代码。这样的方式与我提到的那位php.net网友做法一致,无论关联数组或者索引数组都是一样可以行的。用 'color' => http_build_query(array('red', 'green', 'blue'))这样应该是不行的,从服务器抓到的数据来看,color的值变成了类似0=red&1=green&2=blue这样的结构,预期的应该是数组值。conqweal:谢谢你的热心帮助,确实如你代码所示,只要按照这样组织代码就可以了。感谢各位 <?php echo 111111 ?> 有做过易宝,支付宝等支付流程的童鞋过来看看? 如何在一个js文件中用到一个php文件中的变量的值? 关于PHP文件的机制问题! 来人啊 帮我看看 php readdir apache自动重启,搞不定 大家使用eWebEditor1時有沒有遇到這樣的問題,賦值給他時顯示"undefined" 字符串比较函数调用出错,补救! 听说最新的vbb采用了xml输出来大幅度提高了效率? 我的apache不能启动了,谁来帮忙? php学习,新手求助 织梦系统做的网站
写作
'color' => http_build_query(array('red', 'green', 'blue')),
不行吗?写成这样更好
'color[0]' => 'red',
'color[1]' => 'green',
'color[2]' => 'blue',
$post_data = array(
'a[1]' => 'red',
'b[2]' => 'red',
'c[3]' => 'red',
'e[1]' => "@d:\img.gif",
'e[2]' => "@d:\\2.gif"
);
//通过curl模拟post的请求;
function SendDataByCurl($url,$data=array()){
//对空格进行转义
$url = str_replace(' ','+',$url);
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_TIMEOUT,3); //定义超时3秒钟
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, ($data)); //所需传的数组用http_bulid_query()函数处理一下,就ok了
//执行并获取url地址的内容
$output = curl_exec($ch);
$errorCode = curl_errno($ch);
//释放curl句柄
curl_close($ch);
if(0 !== $errorCode) {
return false;
}
return $output;}
$url = "http://localhost/test/test1.php";
//通过curl的post方式发送接口请求
echo SendDataByCurl($url,$post_data);?>
Array
(
[a] => Array
(
[1] => red
) [b] => Array
(
[2] => red
) [c] => Array
(
[3] => red
))
Array
(
[e] => Array
(
[name] => Array
(
[1] => img.gif
[2] => 2.gif
) [type] => Array
(
[1] => application/octet-stream
[2] => application/octet-stream
) [tmp_name] => Array
(
[1] => C:\Windows\Temp\php5C9E.tmp
[2] => C:\Windows\Temp\php5C9F.tmp
) [error] => Array
(
[1] => 0
[2] => 0
) [size] => Array
(
[1] => 43
[2] => 43
) ))
使用
'color[0]' => 'red',
'color[1]' => 'green',
'color[2]' => 'blue',
这样的方式是可以的,感谢你的提醒,使我再次确认了一下代码。这样的方式与我提到的那位php.net网友做法一致,无论关联数组或者索引数组都是一样可以行的。用 'color' => http_build_query(array('red', 'green', 'blue'))这样应该是不行的,从服务器抓到的数据来看,color的值变成了类似0=red&1=green&2=blue这样的结构,预期的应该是数组值。
conqweal:
谢谢你的热心帮助,确实如你代码所示,只要按照这样组织代码就可以了。
感谢各位