将PHP-CURL上载到Windows WCF之前和之后的文件大小差异
我正在尝试使用PHP上传一个文件。不是那个凝灰岩吧?嗯,我需要通过Webservices WCF来完成。现在我被最后一部分难住了。当我上传文件时,它是:892599(这是Windows和Linux在文件系统上告诉我的,也是PHP告诉我的。但是,当它到达现场时(Windows Server,WCF),它只有891400 这肯定与编码或操作系统的差异有关,但我现在不知道该去哪里看。下面是我的PHP代码 正如您在代码中看到的,我尝试了不同的方法,但结果都是一样的。文件大小不同。源文件(我尝试上载的文件是文本/纯us\U ascii编码的文件)将PHP-CURL上载到Windows WCF之前和之后的文件大小差异,php,web-services,wcf,file-upload,filesize,Php,Web Services,Wcf,File Upload,Filesize,我正在尝试使用PHP上传一个文件。不是那个凝灰岩吧?嗯,我需要通过Webservices WCF来完成。现在我被最后一部分难住了。当我上传文件时,它是:892599(这是Windows和Linux在文件系统上告诉我的,也是PHP告诉我的。但是,当它到达现场时(Windows Server,WCF),它只有891400 这肯定与编码或操作系统的差异有关,但我现在不知道该去哪里看。下面是我的PHP代码 正如您在代码中看到的,我尝试了不同的方法,但结果都是一样的。文件大小不同。源文件(我尝试上载的文件
试试看{
标题(“内容类型:文本/普通;字符集=utf-8”,真);
$filename=“ahtrassed.obj”;
$file\u content=file\u get\u contents($filename);
$file\u hash=hash\u file('sha512',$filename,false);
$bits=pack(“H*”,$file\u散列);
$file\u hash=base64\u encode($bits);
$filesize=strlen($file_content);//filesize($filename);//strlen(file_get_contents($filename));
$boundary=“uuid:”.uniqid();
$headers=数组(
'MIME版本:1.0',
'内容类型:多部分/相关;Type=“application/xop+xml”start=“”;boundary=“”.$boundary.”start info=“text/xml”,
“SOAPAction:”.$action.“,
'主机:www.url.com',
“内容长度:”。$filesize,
'接受编码:gzip,deflate',
'用户代理:PHP Post FileUpload'
);
$post_data=“\r\n\r\n\r\n--{$boundary}\r\n”;
$post_data.=“内容ID:\r\n”;
$post_data.=“内容传输编码:8位\r\n”;
$post_data.='Content Type:application/xop+xml;charset=utf-8;Type=“text/xml”;
$post\U data.=“\r\n\r\n”;
$post_数据='';
$post_数据='';
$post_data.=''.$file_hash';
$post_data.=''.$filename';
$post_data.=''.$filesize';
$post_data.=''.$domain';
$post_数据=“$gebruiker.”;
$post_data.=''.$encrypted_password'.';
$post_数据='';
$post_数据='';
$post_数据='';
$post_数据='';
$post_数据='';
$post_数据='';
$post_数据='';
$post_数据='';
$post_数据='';
$post_data.=“\r\n--{$boundary}\r\n”;
$post_data.=“内容ID:\r\n”;
$post_data.=“内容传输编码:二进制\r\n”;
$post_data.=“内容类型:应用程序/八位字节流\r\n\r\n”;
$post_data.=$file_content.$file_content;
$post_data.=“--{$boundary}\r\n”;
$sock=fsockopen(“ssl://www.url.com“,443,$errorno,$error,30)或die($error);
$data=“POSThttps://www.url.com/FilesTransfer.svc “HTTP/1.1\r\n”;
$data.=内爆(“\r\n”,$headers);
$data.=$post_数据;
如果($sock){
fwrite($sock,$data);
echo fread($sock,strlen($data));
fflush($袜子);
fclose($sock);
}
/*
$ch=curl\u init($url);
卷曲设置($ch,卷曲设置桩,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,30);
curl_setopt($ch,CURLOPT_头,1);
curl_setopt($ch,CURLOPT_VERBOSE,1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$response=curl\u exec($ch);
$error=curl\u error($ch);
打印前($response);
打印前($error);
卷曲关闭($ch);
*/
}捕获(例外$e){
打印(“”);
印刷品(港币);;
打印(“”);
}
所以这个问题的解决方法很简单。它是一个多部分请求,这意味着当您设置“内容长度”时,您应该计算请求的完整大小。因此,XML部分和您尝试上载的文件的大小
请求现在将如下所示:
试试看{
$file_content=utf8_encode(file_get_contents($filename));
$hash=hash('sha512',$file\u content,false);
$bits=pack(“H*”,$hash);
$file\u hash=base64\u encode($bits);
$filesize=filesize($filename);//filesize($filename);//strlen(file_get_contents($filename));
$boundary=“uuid:”.uniqid();
$xml_请求=“”;
$xml_请求='';
$xml_request.=''.$file_hash';
$xml_request.=''.$filename';
$xml_request.=''.$filesize';
$xml_request.=''.$this->domain';
$xml_request.=''.$this->gebruiker';
$xml_请求。=''.$this->encrypted_密码';
$xml_请求='';
$xml_请求='';
$xml_请求='';
$xml_请求='';
$xml_请求='';
$xml_请求='';
$xml_请求='';
$xml_请求='';
$xml_请求='';
$headers=数组(
'MIME版本:1.0',
'内容类型:多部分/相关;Type=“application/xop+xml”start=“”;boundary=“”.$boundary.”start info=“text/xml”,
“SOAPAction:”http://www.url.com/UploadFile"',
'主机:www.url.com',
'接受编码:gzip,deflate',
“连接:保持活动”
);
$post_data=“-->$boundary}\r\n”;
$post_data.=“内容ID:\r\n”;
$post_data.=“内容传输编码:8位\r\n”;
$post_data.='Content Type:application/xop+xml;charset=utf-8;Type=“text/xml”;
$post\U data.=“\r\n\r\n”;
$post_data.=$xml_请求;
$post_data.=“\r\n--{$boundary}\r\n”;
$post_data.=“内容ID:\r\n”;
try{
header("Content-type: text/plain; charset=utf-8", true);
$filename = "ahstray.obj";
$file_content = file_get_contents($filename);
$file_hash = hash_file('sha512', $filename, false);
$bits = pack("H*", $file_hash);
$file_hash = base64_encode($bits);
$filesize = strlen($file_content); //filesize($filename); //strlen(file_get_contents($filename));
$boundary = "uuid:".uniqid();
$headers = array(
'MIME-Version: 1.0',
'Content-Type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="'.$boundary.'";start-info="text/xml"',
'SOAPAction: "'.$action.'"',
'Host: www.url.com',
'Content-length: '.$filesize,
'Accept-Encoding: gzip, deflate',
'User-Agent: PHP-Post-FileUpload'
);
$post_data = "\r\n\r\n\r\n--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/0>\r\n";
$post_data .= "Content-Transfer-Encoding: 8bit\r\n";
$post_data .= 'Content-Type: application/xop+xml;charset=utf-8;type="text/xml"';
$post_data .= "\r\n\r\n";
$post_data .= '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
$post_data .= '<s:Header>';
$post_data .= '<h:FileHash xmlns:h="http://www.materialise.be/eRP">'.$file_hash.'</h:FileHash>';
$post_data .= '<h:FileName xmlns:h="http://www.materialise.be/eRP">'.$filename.'</h:FileName>';
$post_data .= '<h:FileSize xmlns:h="http://www.materialise.be/eRP">'.$filesize.'</h:FileSize>';
$post_data .= '<h:UserDomain xmlns:h="http://www.materialise.be/eRP">'.$domain.'</h:UserDomain>';
$post_data .= '<h:UserName xmlns:h="http://www.materialise.be/eRP">'.$gebruiker.'</h:UserName>';
$post_data .= '<h:UserPassword xmlns:h="http://www.materialise.be/eRP">'.$encrypted_password.'</h:UserPassword>';
$post_data .= '</s:Header>';
$post_data .= '<s:Body>';
$post_data .= '<UploadFileDTO xmlns="http://www.url.com/eRP">';
$post_data .= '<FileStream>';
$post_data .= '<xop:Include href="cid:http://tempuri.org/1/635551016730489495" xmlns:xop="http://www.w3.org/2004/08/xop/include" />';
$post_data .= '</FileStream>';
$post_data .= '</UploadFileDTO>';
$post_data .= '</s:Body>';
$post_data .= '</s:Envelope>';
$post_data .= "\r\n--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/1/635551016730489495>\r\n";
$post_data .= "Content-Transfer-Encoding: binary\r\n";
$post_data .= "Content-Type: application/octet-stream\r\n\r\n";
$post_data .= $file_content.$file_content;
$post_data .= "--{$boundary}\r\n";
$sock = fsockopen("ssl://www.url.com", 443, $errorno, $error, 30) or die($error);
$data = "POST https://www.url.com/FilesTransfer.svc HTTP/1.1\r\n";
$data .= implode("\r\n", $headers);
$data .= $post_data;
if($sock){
fwrite($sock, $data);
echo fread($sock, strlen($data));
fflush($sock);
fclose($sock);
}
/*
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error = curl_error($ch);
print_r_pre($response);
print_r_pre($error);
curl_close($ch);
*/
} catch (Exception $e){
print('<pre>');
print_r($e);
print('</pre>');
}
try{
$file_content = utf8_encode(file_get_contents($filename));
$hash = hash('sha512', $file_content, false);
$bits = pack("H*", $hash);
$file_hash = base64_encode($bits);
$filesize = filesize($filename); //filesize($filename); //strlen(file_get_contents($filename));
$boundary = "uuid:".uniqid();
$xml_request = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">';
$xml_request .= '<s:Header>';
$xml_request .= '<h:FileHash xmlns:h="http://www.materialise.be/eRP">'.$file_hash.'</h:FileHash>';
$xml_request .= '<h:FileName xmlns:h="http://www.materialise.be/eRP">'.$filename.'</h:FileName>';
$xml_request .= '<h:FileSize xmlns:h="http://www.materialise.be/eRP">'.$filesize.'</h:FileSize>';
$xml_request .= '<h:UserDomain xmlns:h="http://www.materialise.be/eRP">'.$this->domain.'</h:UserDomain>';
$xml_request .= '<h:UserName xmlns:h="http://www.materialise.be/eRP">'.$this->gebruiker.'</h:UserName>';
$xml_request .= '<h:UserPassword xmlns:h="http://www.materialise.be/eRP">'.$this->encrypted_password.'</h:UserPassword>';
$xml_request .= '</s:Header>';
$xml_request .= '<s:Body>';
$xml_request .= '<UploadFileDTO xmlns="http://www.url.com/eRP">';
$xml_request .= '<FileStream>';
$xml_request .= '<xop:Include href="cid:http://tempuri.org/1/635551016730489495" xmlns:xop="http://www.w3.org/2004/08/xop/include" />';
$xml_request .= '</FileStream>';
$xml_request .= '</UploadFileDTO>';
$xml_request .= '</s:Body>';
$xml_request .= '</s:Envelope>';
$headers = array(
'MIME-Version: 1.0',
'Content-Type: multipart/related; type="application/xop+xml";start="<http://tempuri.org/0>";boundary="'.$boundary.'";start-info="text/xml"',
'SOAPAction: "http://www.url.com/UploadFile"',
'Host: www.url.com',
'Accept-Encoding: gzip, deflate',
'Connection: Keep-Alive'
);
$post_data = "--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/0>\r\n";
$post_data .= "Content-Transfer-Encoding: 8bit\r\n";
$post_data .= 'Content-Type: application/xop+xml;charset=utf-8;type="text/xml"';
$post_data .= "\r\n\r\n";
$post_data .= $xml_request;
$post_data .= "\r\n--{$boundary}\r\n";
$post_data .= "Content-ID: <http://tempuri.org/1/635551016730489495>\r\n";
$post_data .= "Content-Transfer-Encoding: binary\r\n";
$post_data .= "Content-Type: application/octet-stream\r\n\r\n";
$headers[] = "Content-length: ".($filesize+strlen($post_data));
$post_data .= $file_content;
$post_data .= "--{$boundary}\r\n";
$ch = curl_init($this->wsdl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
$error = curl_error($ch);