在 PHP 中通过 SOAP 发送 Zip 文件

Sending Zip file via SOAP in PHP

我必须将文件发送到 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);

而且它没有任何问题。

谢谢!

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);

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

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