czim/laravel-service

Set a Body in the Soap Request

Closed this issue · 6 comments

Hi, its me again with a question.

In my laravel project I'm declaring an specific request for a service I'm creating, I compare both request of Global Weather service and mine, and I notice some changes:

Here is the request of my service

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mul="http://MULEAutentificarLDAPCorporativo/" xmlns:mul1="http://MULEAutentificarLDAPCorporativo">
   <soapenv:Header/>
   <soapenv:Body>
      <mul:autentificarLDAP>
         <!--Optional:-->
         <mul:arg0>
            <!--Optional:-->
            <mul1:IDCLIENTEPIC></mul1:IDCLIENTEPIC>
            <!--Optional:-->
            <mul1:password>$$</mul1:password>
            <!--Optional:-->
            <mul1:userName></mul1:userName>
         </mul:arg0>
      </mul:autentificarLDAP>
   </soapenv:Body>
</soapenv:Envelope>

And this is the request of the global weather

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetWeather>
         <!--Optional:-->
         <web:CityName>Tokyo</web:CityName>
         <!--Optional:-->
         <web:CountryName>Japan</web:CountryName>
      </web:GetWeather>
   </soapenv:Body>
</soapenv:Envelope>

The problem I'm having is I'm sending to the request of my service for some reason.

This the setBody of my project
        $requestSOAP = new ServiceSoapRequest();
        $requestSOAP->setBody([
            'IDCLIENTEPIC'    => 'picdev',
            'password' => $request->password,
            'userName' => $request->name,
        ]);

And here is the log of the service I'm consulting in:

DEBUG 2017-11-16 13:51:01,364  SOAPSERVICE: (Session=544) Request elements
INFO  2017-11-16 13:51:01,364  SOAPSERVICE: (Session=544) <null/>
ERROR 2017-11-16 13:51:01,365  SOAPSERVICE: (Session=544)  Client ID ingresado: 12345
ERROR 2017-11-16 13:51:01,439  SOAPSERVICE: (Session=544) Exception :9007 org.xml.sax.SAXException: Invalid element in ConexAutentificarLDAPCorporativo.CheckAccountResponse - mensaje

And this is how it should send the request:

DEBUG 2017-11-16 13:57:15,857  SOAPSERVICE: (Session=548) Request elements
INFO  2017-11-16 13:57:15,858  SOAPSERVICE: (Session=548) 
<MULEAutentificarLDAPCorporativo.AutentificarLDAPRQ>
  <IDCLIENTEPIC>picdev</IDCLIENTEPIC>
  <UserName>username</UserName>
  <Password>secret</Password>
</MULEAutentificarLDAPCorporativo.AutentificarLDAPRQ>
ERROR 2017-11-16 13:57:15,859  SOAPSERVICE: (Session=548)  Client ID ingresado: 12345

How I can change the ServiceSoapRequest?

czim commented

What code do you use to make the actual call on the service? How do you pass in the ServiceSoapRequest that you've built up?

Well... my ServiceSoapRequest is empty. I have to add something in that class? And if it so... Where I can find an example ?

czim commented

I'm sorry, I'm sure it's me, but I can't really figure out what's going wrong -- and how it would look if it were going right. Do you have the code shared somewhere, so I can get a look at it in context?

Don't worry, I'll try to explain what I'm trying to do.

This is my default set up

        // Set up defaults
        $defaults = new \Czim\Service\Requests\ServiceSoapRequestDefaults();
        $defaults->setLocation('WSDLEXAMPLE?wsdl')
            ->setOptions([
                'trace'      => true,
                'exceptions' => true,
                'features'   => SOAP_SINGLE_ELEMENT_ARRAYS,
            ]);

This is my instantiate service, with a to-array interpreter

        $service = new SoapService(
            $defaults,
            new \Czim\Service\Interpreters\BasicSoapXmlAsArrayInterpreter()
        );

This is my request, I guess, this is where the problem is happening.

        $requestSOAP = new ServiceSoapRequest();
        $requestSOAP->setBody([
            'IDCLIENTEPIC'    => 'picdev',
            'password' => $request->password,
            'userName' => $request->name,
        ]);

This is my call, which will return a ServiceReponse object

        $response = false;
        $response = $service->call('autentificarLDAP', $requestSOAP);

The problem I'm having is my Request, because, in my Log of my WSDL, the request is </null> for some reason. This is how the request should look like in the log, this is ocurring because I'm testing in SoapUI.

DEBUG 2017-11-16 13:57:15,857  SOAPSERVICE: (Session=548) Request elements
INFO  2017-11-16 13:57:15,858  SOAPSERVICE: (Session=548) 
<MULEAutentificarLDAPCorporativo.AutentificarLDAPRQ>
  <IDCLIENTEPIC>picdev</IDCLIENTEPIC>
  <UserName>username</UserName>
  <Password>secret</Password>
</MULEAutentificarLDAPCorporativo.AutentificarLDAPRQ>
ERROR 2017-11-16 13:57:15,859  SOAPSERVICE: (Session=548)  Client ID ingresado: 12345

Finally, my question is: How I can send exactly this request in my $service->call?

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mul="http://MULEAutentificarLDAPCorporativo/" xmlns:mul1="http://MULEAutentificarLDAPCorporativo">
   <soapenv:Header/>
   <soapenv:Body>
      <mul:autentificarLDAP>
         <!--Optional:-->
         <mul:arg0>
            <!--Optional:-->
            <mul1:IDCLIENTEPIC>picdev</mul1:IDCLIENTEPIC>
            <!--Optional:-->
            <mul1:password>Secret</mul1:password>
            <!--Optional:-->
            <mul1:userName>username</mul1:userName>
         </mul:arg0>
      </mul:autentificarLDAP>
   </soapenv:Body>
</soapenv:Envelope>

I figure out. Its an associative array inside of another associative array...

arg0 is storing the 'IDCLIENTEPIC' => 'picdev', 'password' => $request->password, 'userName' => $request->name, in order to make it worth you have to do this:

Create an associative array:

        $body= [
              'IDCLIENTEPIC'    => 'picdev',
              'password' => $request->password,
              'userName' => $request->name,
        ];

Set the body:

        $requestSOAP->setBody([
          'arg0' => $body,
        ]);

Why? Because in my Request I have de $body inside an arg0 I guess this is a particular case because my web service has that structure.

czim commented

Ah, that makes sense, I guess. I couldn't figure out what the problem could be related to the package itself, but body structure can vary a lot. Glad you figured it out! 👍