curl http post 数据和图片
2010-11-17 14:47
add_post_tips($url,$port=80,$content,$upload_arr){
$boundray = time();
$h = array();
$m_line="\r\n";
//准备头和XML文档
$h[] = "Content-Type: multipart/form-data; boundary=".$boundray;
$h[] = "Connection: Keep-Alive";
$t = array();
$t[] = "--".$boundray;
$t[] = "Content-Disposition: form-data; name=\"UPLOAD_IDENTIFIER\"".$m_line;
$t[] = "34f4eec9d2922ab59d23c8b02291dd4f";
$t[] = "--".$boundray;
$t[] = "Content-Disposition: form-data; name=\"json\"".$m_line;
$t[] = $content;
$t[] = "--".$boundray;
$t[] = "Content-Disposition: form-data; name=\"File_1\"; filename=\"".$upload_arr['name']."\"";
$t[] = "Content-Type: image/jpeg".$m_line.$m_line;
$c = implode($m_line,$t);
//准备二进制内容
$upload_fp = fopen($upload_arr['tmp_name'], "rb");
while(!feof($upload_fp)) {
$file_piece = fread($upload_fp, 4096);
$c .= $file_piece;
}
fclose($upload_fp);
$c .=$m_line."--".$boundray;
// create a new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_HTTPHEADER, $h);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $c);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);
return ($rsp);
}
$boundray = time();
$h = array();
$m_line="\r\n";
//准备头和XML文档
$h[] = "Content-Type: multipart/form-data; boundary=".$boundray;
$h[] = "Connection: Keep-Alive";
$t = array();
$t[] = "--".$boundray;
$t[] = "Content-Disposition: form-data; name=\"UPLOAD_IDENTIFIER\"".$m_line;
$t[] = "34f4eec9d2922ab59d23c8b02291dd4f";
$t[] = "--".$boundray;
$t[] = "Content-Disposition: form-data; name=\"json\"".$m_line;
$t[] = $content;
$t[] = "--".$boundray;
$t[] = "Content-Disposition: form-data; name=\"File_1\"; filename=\"".$upload_arr['name']."\"";
$t[] = "Content-Type: image/jpeg".$m_line.$m_line;
$c = implode($m_line,$t);
//准备二进制内容
$upload_fp = fopen($upload_arr['tmp_name'], "rb");
while(!feof($upload_fp)) {
$file_piece = fread($upload_fp, 4096);
$c .= $file_piece;
}
fclose($upload_fp);
$c .=$m_line."--".$boundray;
// create a new cURL resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, $port);
curl_setopt($ch, CURLOPT_HTTPHEADER, $h);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $c);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$rsp = curl_exec($ch);
return ($rsp);
}