在PHP中通过SOAP发送Zip文件

时间:2015-02-26 02:25:32

标签: c# php web-services soap wsdl

我必须将文件发送到WSDL,该元素在WSDL中描述为:

<s:element minOccurs="0" maxOccurs="1" name="theZipFile" type="s:base64Binary" />

如何使用SOAP客户端发送Zip文件?我尝试过以下方法:

$client = new SoapClient($url);
$params = array("theZipFile" => "file.zip");
$response = $client->theFunction($params);

但我没有得到预期的回应。我尝试使用.Net和C#,代码如下:

string filename = "file.zip";
FileInfo fi = new FileInfo(filename);
long numBytes = fi.Length;
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] data = br.ReadBytes((int)numBytes);
br.Close();
fs.Close();

XElement response = client.theFunction(data);

它没有任何问题。

谢谢!

2 个答案:

答案 0 :(得分:1)

SoapClient没有因某些奇怪的原因发送正确的XML,显然这个定义存在问题。

更改为使用CURL。

function SOAPRawRequest($url, $postString, &$error) {
  $soap_do = curl_init(); 
  curl_setopt($soap_do, CURLOPT_URL,            $url );   
  curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); 
  curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
  curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
  curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);  
  curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
  curl_setopt($soap_do, CURLOPT_POST,           true ); 
  curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $postString); 
  curl_setopt($soap_do, CURLOPT_HTTPHEADER,     
    array('Content-Type: text/xml; charset=utf-8', 
      "Accept: text/xml",
      "Cache-Control: no-cache",
      "Pragma: no-cache",
      "SOAPAction: \"http://tempuri.org/theFunction\"",
      'Content-Length: '.strlen($postString)
    ));

  $result = curl_exec($soap_do);
  $error = curl_error($soap_do);

  return $result;
}

还将$params = array("theZipFile" => "file.zip");更改为:

$content = file_get_contents("file.zip");
$content64 = base64_encode($content);

答案 1 :(得分:1)

您正在将文件名而不是文件内容传递给soap调用。使用

$params = array("theZipFile" => base64_encode(file_get_contents('path/to/a/file.zip')));