PHP multipart/form-data 二維數組 多維數組 文件傳送 例程 數組
樓主費勁千辛萬苦 才弄出來的 app
$data = array(
'name_list'=> array('LingMax'=>12,'lisi'=>12,'zhaoer'=>12),
'age_list'=>array('16','14','19'),
'name'=>'person'
);
$url = "http://www.05gzs.com/";
http_post_buildData($url,$data);
#加班的接口
function http_post_buildData($url,$param){
#手工組包
$data = '';
$id = uniqid();
buildData_fun($param,$id,$data);
$data .= "--".$id . "--";
#手工組包
echo $data;
$ch = curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);//設置URL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//設置容許https訪問。忽略證書錯誤
curl_setopt($ch,CURLOPT_POST,1);//設置get或者post
curl_setopt($ch,CURLOPT_HTTPHEADER,[
'Expect: ',//PHP的坑 設置POST不發送 HTTP/1.1 100 Continue
"Content-Type: multipart/form-data; boundary=".$id,
"Content-Length: " . strlen($data)
]);//設置協議附加頭head
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//提交POST內容
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//在執行curl_exec返回結果
$ret=curl_exec($ch);//執行
curl_close($ch);
return $ret;
}
#手工組包
function buildData_fun($param,$id,&$data,&$path='',$iii=0){
if(isset($param['filename'])){
#正常時是這樣傳送file文件的
#$file = curl_file_create(ROOT_PATH . '/public/154106488401000050.pdf',"application/octet-stream");
$data .= "--".$id . "\r\n"
. 'Content-Disposition: form-data; name="'. $path.'"; filename="' . $param['filename'] . '"' . "\r\n"
. 'Content-Type: application/octet-stream'."\r\n\r\n";
$data .= $param['file'] . "\r\n";
return ;
}
if(is_array($param) && $path != ''){
$data .= "--" . $id . "\r\n". 'Content-Disposition: form-data; name="' .$path . "\"\r\n\r\n". $content . "\r\n";
}
foreach ($param as $name => $content) {
if($path == ''){
$path1 = $name;
}else{
$path1 = $path."[".$name."]";
}
if(is_array($content)) {
buildData_fun($content,$id,$data,$path1);
} else{
$data .= "--" . $id . "\r\n"
. 'Content-Disposition: form-data; name="' .$path1 . "\"\r\n\r\n"
. $content . "\r\n";
}
}
return ;
}
curl