【问题标题】:Sending Zip file via SOAP in PHP在 PHP 中通过 SOAP 发送 Zip 文件
【发布时间】:2015-02-26 02:25:31
【问题描述】:

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

它可以正常工作。

谢谢!

【问题讨论】:

标签: c# php web-services soap wsdl


【解决方案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" =&gt; "file.zip"); 更改为:

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

【讨论】:

  • 那么是更改为 CURL 还是将字节实际编码为 base64?似乎原始代码会发送原始二进制文件,这会破坏/使您的 XML 无效。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 2011-03-01
    • 2013-04-07
    相关资源
    最近更新 更多