december 13, 2014 at 1:15 pm

PHP SoapClient with cURL powered request

I stumbled upon a SOAP related problem at work recently; I needed to consume a SOAP web service secured by two way authentication via self signed SSL certificates (…). This service was provided by another department so there was no arguing whether to use SOAP or, my personal preference, REST.

My project is build out of two components; An AngularJS front-end webapp and a CakePHP powered REST API. The CakePHP API created one endpoint for the front-end clients and connected them to all sorts of different data-sources (MongoDB, local MySQL, remote MySQL and a SOAP service).

So, back to the problem (and solution) at hand; For SOAP I started using PHP’s native SoapClient class. This works pretty nice if you have the WSDL and XSD files for the service, it even should work with SSL certificates, private keys and passphrases… except it doesn’t… For some reason (PHP 5.3, don’t ask…) the requirement of the service to pass both a client certificate as well as a private key with passphrase doesn’t work (failed to enable crypto error).

After some searching I kept finding a workaround using cURL to make the SOAP request so I went with that and could indeed make the SOAP call. The result unfortunately was, of course, raw XML I had to parse manually to get my data. Apart from that I also had to create the SOAP envelope and request body myself by just creating a string instead of using the data param in the SoapClient way.

Ok, enough problems and inconveniences lets get to the solutions!

I extended the SoapClient class and overwrote the __doRequest method with a cURL powered method body, returning the raw XML of the result afterwards lets the SoapClient class parse it and validate it against the XSD, this way you have the best of both worlds; The power and ease of the SoapClient class and the flexibility of cURL for the actual request.

The code is posted on GitHub as a gist including an example:



0 likes php , webservices # , , , , , , ,
Share: / / /

5 thoughts on “PHP SoapClient with cURL powered request

  1. Roberto schreef:

    Thanks for your share.

    I am newbie with this. Sorry for my question but what do you add the kye, cert and wsdl file

    1. stefan schreef:

      Roberto, I dont understand the ‘what’ in your question, do you mean ‘why’ or ‘where’?

  2. Roberto schreef:

    Where I mean.

    Thanks for your help.

    1. stefan schreef:

      Roberto, the second snippet is an example call creating an options array containing (absolute) filesystem paths to the WSDL, cert and key files.

      I’ll add some comments in the snippet to illustrate.

  3. Luis Cordova schreef:

    so is this problem also present for upward versions of php?

    please ping me at my email above

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Verplichte velden zijn gemarkeerd met *