3

I have to send a file to a WSDL, the element is described in the WSDL as:

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

How can I send the Zip file using SOAP Client? I have tried the following:

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

But I don't get the expected response. I have tried using .Net and C# with the following code:

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

And it works without any problem.

Thanks!

JohnnyAce
  • 3,569
  • 9
  • 37
  • 59
  • 1
    Where are you converting the byte stream to base 64 and did you verify that the zip file sent in the working code was still able to be opened? I think the .NET is automatically converting to base64 in contrast to PHP where you would have to do that manually. – Sean Anderson Feb 28 '15 at 15:42
  • 1
    This should help http://stackoverflow.com/questions/35879/base64-encoding-image – Sean Anderson Feb 28 '15 at 15:48

2 Answers2

1

SoapClient wasn't sending the correct XML for some strange reason apparently was a problem with the definition.

Changed to use 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;
}

Also changed $params = array("theZipFile" => "file.zip"); to:

$content = file_get_contents("file.zip");
$content64 = base64_encode($content);
JohnnyAce
  • 3,569
  • 9
  • 37
  • 59
  • So was it the change to CURL or the actual encoding of the bytes to base64? Seems as though the original code would have sent raw binary which would have destroyed/invalidated your XML. – Sean Anderson Feb 28 '15 at 20:08
1

You are passing a file name instead of file content to the soap call. Use

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