淘宝SDK扒出来的CURL调用含文件上传代码
<?php function curl($url,$postFields=null){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_FAILONERROR,false); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); //if($this->readTimeout){ curl_setopt($ch,CURLOPT_TIMEOUT,300); //} //if($this->connectTimeout){ curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,300); //} curl_setopt($ch,CURLOPT_USERAGENT,"top-sdk-php"); //https 请求 if(strlen($url)>5&&strtolower(substr($url,0,5))=="https"){ curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); } //POST if(is_array($postFields)&&0<count($postFields)){ $postBodyString=""; $postMultipart=false; foreach($postFields as $k=>$v){ if(!is_string($v)) continue ; //判断是不是文件上传 if("@"!=substr($v,0,1)){ $postBodyString.="$k=".urlencode($v)."&"; }else{ //文件上传用multipart/form-data,否则用www-form-urlencoded $postMultipart=true; if(class_exists('\CURLFile')){ $postFields[$k]=new \CURLFile(substr($v,1)); } } } unset($k,$v); curl_setopt($ch,CURLOPT_POST,true); if($postMultipart){ if(class_exists('\CURLFile')) { curl_setopt($ch,CURLOPT_SAFE_UPLOAD,true); }else{ if(defined('CURLOPT_SAFE_UPLOAD')){ curl_setopt($ch,CURLOPT_SAFE_UPLOAD,false); } } curl_setopt($ch,CURLOPT_POSTFIELDS,$postFields); }else{ $header=array("content-type:application/x-www-form-urlencoded;charset=UTF-8"); curl_setopt($ch,CURLOPT_HTTPHEADER,$header); curl_setopt($ch,CURLOPT_POSTFIELDS,substr($postBodyString,0,-1)); } } $reponse=curl_exec($ch); if (curl_errno($ch)){ throw new Exception(curl_error($ch),0); }else{ $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if(200!==$httpStatusCode){ throw new Exception($reponse,$httpStatusCode); } } curl_close($ch); return $reponse; } function curl_with_memory_file($url, $postFields = null, $fileFields = null){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_FAILONERROR,false); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); //if($this->readTimeout){ curl_setopt($ch,CURLOPT_TIMEOUT,300); //} //if($this->connectTimeout){ curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,300); //} curl_setopt($ch,CURLOPT_USERAGENT,"top-sdk-php" ); //https 请求 if(strlen($url)>5&&strtolower(substr($url,0,5))=="https"){ curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); } //生成分隔符 $delimiter='-------------'.uniqid(); //先将post的普通数据生成主体字符串 $data=''; if($postFields!=null){ foreach($postFields as $name=>$content) { $data.="--".$delimiter."\r\n"; $data.='Content-Disposition:form-data;name="'.$name.'"'; //multipart/form-data 不需要urlencode,参见 http:stackoverflow.com/questions/6603928/should-i-url-encode-post-data $data.="\r\n\r\n".$content."\r\n"; } unset($name,$content); } //将上传的文件生成主体字符串 if($fileFields!=null){ foreach($fileFields as $name=>$file) { $data.="--".$delimiter."\r\n"; $data.='Content-Disposition:form-data; name="'.$name.'";filename="'.$file['name']."\"\r\n"; $data.='Content-Type:'.$file['type']."\r\n\r\n";//多了个文档类型 $data.=$file['content']."\r\n"; } unset($name,$file); } //主体结束的分隔符 $data.="--".$delimiter."--"; curl_setopt($ch,CURLOPT_POST,true); curl_setopt($ch,CURLOPT_HTTPHEADER,array( 'Content-Type: multipart/form-data;boundary='.$delimiter, 'Content-Length:'.strlen($data)) ); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_POSTFIELDS,$data); $reponse=curl_exec($ch); unset($data); if(curl_errno($ch)){ throw new Exception(curl_error($ch),0); }else{ $httpStatusCode=curl_getinfo($ch,CURLINFO_HTTP_CODE); if (200!==$httpStatusCode){ throw new Exception($reponse,$httpStatusCode); } } curl_close($ch); return $reponse; } print_r(curl_with_memory_file("http://localhost/aa.php", array("a"=>1,"b"=>2), array( array( "name"=>"a.png", "type"=>"application/x-png", "content"=>file_get_contents("http://pic002.cnblogs.com/images/2011/42876/2011050119000033.png") ), array( "name"=>"b.png", "type"=>"application/x-png", "content"=>file_get_contents("http://pic002.cnblogs.com/images/2011/42876/2011050119000033.png") ) ) )); print_r(curl("http://localhost/aa.php",array("a"=>1,"b"=>2,"c"=>3,"d"=>"@d:/TB1eH7vNFXXXXcLXpXXwu0bFXXX.png"))); ?>