Feed on
Posts

## PHP SOAP Messages with Attachments

This article is to make a simple example on how you can manage SOAP Messages with Attachements in PHP and, in particular, I'll describe how to use the PEAR::SOAP package to transmit files associated to a SOAP message as attachments in their native format in a multipart MIME structure for transport (SOAP Messages with Attachments specifications).

Even if the PEAR::SOAP package is not yet considered stable and even if it lacks completely of documentation, the use of it is the only way I found to solve this problem. Nor the standard SOAP PHP5 implementation, nor the Zend_Soap package in the Zend Framework provides any way to manage attachments to SOAP messages.

The first problem you have with PEAR::SOAP is the lack of documentation, no API documentation, no examples, nothing except what you can find within the source code. So, if you want to use it in the proper way, you have to read and understand the source code and make many many tests.

The example I'll make will invoke an operation that requires a binary file as first parameter. First we have to create a SOAP client using the wsdl of the webservice to invoke. Assuming you have correctly installed the PEAR:SOAP package, the code to create a client is very simple.

PHP:
1. require "SOAP/Client.php";
2.
3. $wsdl = new SOAP_WSDL('http://somehost.com/webservice.wsdl'); 4.$client = $wsdl->getProxy(); Once you have a client for the webservice to invoke it's time to set some options, for example if the target webservice needs an HTTP authentication you have to set user and pass options. PHP: 1.$client->setOpt('user', 'myusername');
2. $client->setOpt('pass', 'mypassword'); Now we assume to have a webservice with a register operation that takes only one parameter and this parameter is an xml file. If the xml file has to be transferrered as an attachment we have first to build a SOAP_Attachment object, then we have to chose the method used by the framework to transport the binary content and finally we have to invoke the register operation passing the created attachment as parameter. PHP: 1.$v = new SOAP_Attachment('contentID', 'text/xml; charset=utf-8', null, $xml); 2.$client->setOpt('Mime', true);
3. $ret =$client->register($v); If you don't have the binary file already loaded in a string, but you have a file on your filesystem, the first line of the last code snippet has to be modified as follows: PHP: 1.$v = new SOAP_Attachment('contentID', 'text/xml; charset=utf-8', 'filename.xml');

### 2 Responses to “PHP SOAP Messages with Attachments”

1. on 23 Jan 2013 at 11:11 amAlessandro

Ciao provando il tuo script ricervo questi errori che ti elenco di seguito
inoltre avrei la necessita di usare un local cert come autentificazione

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\Client.php on line 749

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\Base.php on line 124

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\Base.php on line 747

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\Base.php on line 968

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\Base.php on line 1012

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\Transport.php on line 146

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\WSDL.php on line 215

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\WSDL.php on line 236

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\WSDL.php on line 788

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\WSDL.php on line 1066

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\WSDL.php on line 1149

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\WSDL.php on line 1675

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 412

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 736

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 749

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 794

Strict Standards: Redefining already defined constructor for class Net_URL in C:\wamp\bin\php\php5.4.9\pear\Net\URL.php on line 124

Deprecated: Assigning the return value of new by reference is deprecated in C:\wamp\bin\php\php5.4.9\pear\SOAP\Parser.php on line 242

Strict Standards: Non-static method PEAR::getStaticProperty() should not be called statically, assuming $this from incompatible context in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 724 Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming$this from incompatible context in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 739

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming $this from incompatible context in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 741 Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming$this from incompatible context in C:\wamp\bin\php\php5.4.9\pear\HTTP\Request.php on line 771

Strict Standards: Non-static method PEAR::isError() should not be called statically, assuming \$this from incompatible context in C:\wamp\bin\php\php5.4.9\pear\SOAP\Value.php on line 194

Fatal error: Call to undefined method SOAP_Fault::setOpt() in C:\wamp\www\infocamara\index.php on line 6

2. on 15 Mar 2013 at 11:58 pmpaladinux

ricevi l’ errore perché usi una classe esterna come menzionato nell’esempio.

Puoi usare la classe nativa di php (soapClass), oppure altre classi che hanno mantenuto la compatibilitá con PHP5 tipo la nusoap
trovi tutto qui: http://php.net/manual/en/soapclient.soapclient.php