BeSimple / BeSimpleSoap

NOT MAINTAINED - Build SOAP and WSDL based web services
http://besim.pl
106 stars 137 forks source link

example client with mtom attach #77

Open ilariagiannetti opened 9 years ago

ilariagiannetti commented 9 years ago

Good morning, thanks for the BeSimple bundle, it works great! Since I'm new to using MTOM mechanism and since it's not easy to find php code that supports MTOM attachments in SOAP messages, I would need an example. In particular, i have to implement a SOAP client that sends a message with attachment (using MTOM mechanism) to a WS service listening. My client without attachments works. My problem that i can not manage attachment part, I do not know what methods to call and how. I set the header but I can not dial the message header and attachment. It would be possible to have an example of a client sending a request with MTOM attachment ? I attach my code. Thank you in advance.

--- MY CODE --- $nome = "richiesta_".time().".zip"; $clientbuilder = $this->get('besimple.soap.client.builder.client_name'); $soapClient= $clientbuilder->build();

    $wsdl= $clientbuilder->getWsdl();

    $sc = new BeSimpleSoapClient($wsdl, array(
        'attachment_type' => BeSimpleSoapHelper::ATTACHMENTS_TYPE_MTOM,
        'cache_wsdl'      => WSDL_CACHE_NONE,
        'trace' => 1,
        'exception' => true));

    $ss= new SoapCommon\Mime\Part();
    $ss->setHeader("Content-Type","applicatio/xop+xml");
    $ss->setHeader("type","text/xml");
    $ss->setHeader("Content-Transfer-Encoding","base64");

   $b53 = new SoapCommon\Mime\MultiPart();
    $aa= $ss->getMessagePart();
    $b53->getMimeMessage($aa);

    $soapClient->getSoapKernel()->addAttachment($ss);

    $b53->addPart($ss);

    $sc->getSoapKernel()->addAttachment($ss);

    // HEADER PARAMETER
   // ---------------- my var parameter for header request----------------

    // BODY PARAMETER
     // ---------------- my var parameter for body request----------------

    // HEADER
    $CredentialObjectXML  = '
    <wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
         // ---------------- security part ----------------
    </wsse:Security>';
    $CredentialObject  = new \SoapVar($CredentialObjectXML,XSD_ANYXML);
    $header = new \SoapHeader('http://schemas.xmlsoap.org/soap/envelope/','null',$CredentialObject);
    $soapClient->__setSoapHeaders($header);

    // BODY
    $inputparam = new \SoapVar('
    <ns1:InstanceMessageCreateRequest>
         // ---------------- other body request part ----------------
     <attachments>
        <!--Zero or more repetitions:-->
        <attachment>
           <fileset>attachment</fileset>
           <filename>filename</filename>
           <!--Optional:-->
           <contentType>application</contentType>
           <data>cid</data>
        </attachment>
     </attachments>
             // ---------------- other body request part ----------------
    </ns1:InstanceMessageCreateRequest>', XSD_ANYXML);

    // request call
    try {
        $return = $soapClient->mathod_name($inputparam);
        $sc->getSoapKernel()->filterRequest($return);
    }
    catch (\SoapFault $exception) {

    }

    ));
minimihi commented 8 years ago

Hello, I won't be troubling myself with generating request and adding files "manully" :confused: BeSimple's and PHP's SoapClient pretty much handles it all. Leaving it for BeSimple to handle (+ classmap) would be much simpler and clearer.

But I can point you to files you should investigate, if you want to do it "manually".

BeSimple/SoapCommon/Converter/MtomTypeConverter.php, convertPhpToXml method. You can see how file content element is changed. It generates an XOP:Include element with reference (href) to binary attachment. Also notice MimePart (BeSimple\SoapCommon\Mime\Part) constructor and that attachment is added.

BeSimple/SoapClient/MimeFilter.php, filterRequest method. Filter which adds attachments to request. If you are registering more filters, then this filter must be registered last.

``

Also you can use BeSimple\SoapClient\WsSecurityFilter to add WS-Security headers. Which would look like that

use BeSimple\SoapClient\WsSecurityFilter;

$wssFilter = new WsSecurityFilter($addTimestamp = true, $expires = 300, $actor = null);
$wssFilter->addUserData(
    'username',
    'password',
    WsSecurityFilter::PASSWORD_TYPE_DIGEST // or WsSecurityFilter::PASSWORD_TYPE_TEXT
);
$soapClient->getSoapKernel()->registerFilter($wssFilter);
ilariagiannetti commented 8 years ago

Thanks for the answer and for the detailed information. A further question: if we want to attach the file directly via besimple (not manually), which call should we do? In particular, at what point in the code and how we can indicate the attachment file the client must send? Could you give us an example ? Thank you very much for helping

minimihi commented 8 years ago

BeSimple provides a test for MTOM. BeSimple/SoapClient/Tests/ServerInterop/MTOMClient.php BeSimple/SoapClient/Tests/ServerInterop/MTOMServer.php you might need to update path to composer autoload.php and MTOM.wsdl.

You can start server with shell script BeSimple/SoapClient/Tests/bin/phpwebserver.sh.

If you will still have problems, email me your wsdl. I might prepare sample for it.

MrZANO commented 8 years ago

Sorry @minimihi i have same problem.

I can not generate a MTOM. :(

could you tell me what are the basic steps to create a multipart message to send with curl ?

Help me. Please

Thanks

mssob commented 8 years ago

i need to send a file using MTOM and BeSimple soap with php. Does anyone has an example to show me?

aschamberger commented 8 years ago

As minimihi already mentioned have a look at the tests for examples how to use MTOM: https://github.com/BeSimple/BeSimpleSoap/tree/master/src/BeSimple/SoapClient/Tests/ServerInterop

cmeheut commented 8 years ago

Hello, I'm also trying to use MTOM to send a file (pdf) to a specific server method. I didn't find in the example from minimihi and aschamberger how we can create the body message. I try to understand BeSimple/SoapCommon/Converter/MtomTypeConverter.php but without any documentation it's quite hard to find the solution. did Anybody do that (attached a file content with XOP)? Many thanks if someone can help

mssob commented 8 years ago

Hi,

i sent some files using BeSimple/MTOM and used the BeSimple/SoapClient/Tests/ServerInterop/MTOMClient.php as example. I did not have to understand the way the library works. I put the document content in my classe and call the service using the besimple soap client. BeSimple make the work of mount the mtom request, putting de xop link in the middle of the message and attaching the binary content at the end of the message.

Marcos.

cmeheut commented 8 years ago

Thanks Marcos for your help. I don't try anymore to study the mechanism and work with the example. But I have difficulties to set the Content-Type. (It is Content-Type: application/octet-stream; charset=utf-8 and I would like application/pdf) Any idea of what could be wrong? Thank you if you can help me. Christophe