Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, March 30, 2016

Send SOAP request XML via PHP cURL


At times PHP SOAP can be problematic to work with. This may be due to lack of proper error handling within its internal structure. The SOAP Client class tends to ignore any HTTP error from the server. 

One way to by-pass this limitation with the soap client class is to send the raw soap request XML as http post with PHP cURL. With this, you can see the actual http server response even in situation where the soap client class would have returned nothing.

Enough of the story, let’s see how we can achieve this in practice

Thursday, June 26, 2014

PHP Soap Client Request with SSL Certificate


When required to consume PHP SOAP web services via HTTPS with certificate authentication, we pass our certificate file to the parameter, “local_cert” of the soap client class. In case our certificate and private key are in separate files, we must combine them into one file for this to work.  You can google around on how to create a ‘.pem’ file from a certificate and private key.

The code below is an excerpt from the PHP SOAP Client Example. The point to note here is that we have added the local_cert parameter which contains the value of our certificate file. Most importantly, we have also changed the protocol from http to https for both the wsdl and the soap server location parameters. 

Monday, May 26, 2014

How To Add Header Message To PHP SOAP Client Request

Some web services require header message to be passed along with the body of the soap request. This is often needed for various reasons such as authentication, payment etc. In order to consume such web services successfully, we must define and pass the header element along with the body of the soap request message.

In this tutorial, we will demonstrates how to define and add a SOAP header message to PHP SOAP Client request. You can take a look at the web services we are trying to consume here. A sample request for the method “boom” from the web service is shown below.
From the sample soap request, we noticed that the soap header element, “requestHeader” has two inputs - username and password. We defined these inputs with an associative array. Eg
Next, we used the PHP SoapHeader class to create the soap header by passing it the namespace of the web service (e.g “http://wsdl.example.org/”), the name of the soap header we trying to create (e.g “requestHeader”) and the the soap header inputs defined above (e.g $headerbody).
Finally, we called the __setSoapHeaders($header) method on the soap client object to set the soap header.

See the complete codes below. 


A sample request and response obtained from the above script.

Request:


Response:


Friday, April 4, 2014

How to process Soap header in php SoapServer

To process soap header request in our PHP SOAP Server Example, we will add to our PHP server class, a function with the same name as the header element we are trying to process. Please ensure that you are using the latest version of PHP to avoid any weird behaviour.


See How To Add Header Message To WSDL, on how we created the header element. For this example, our header element is named, requestHeader. So we will create a function  in our server class, with the same name, requestHeader.
The requestHeader element is a complexType with two parameters, which we can process in php as an associative array with two keys.



Our requestHeader function will be called if soap request contains "requestHeader" in SOAP header. For demonstration, we simply stored the requestHeader parameters, username and password, in a private variable, headerInfo, and returned this info back to the client in the boom() function. See the complete server codes below;


The updated client version, on how to add SOAP Header request to PHP SOAP Client, will be available soon.

Friday, March 21, 2014

A Complete PHP SOAP Client Example

This is a complete PHP SOAP Client example where we consumed the web services we developed here. You can also take a look at how we generated the WSDL file for the web service.



The code below started by disabling WSDL cache and defined the wsdl location. We also specify the soap server location. This parameter is optional if you define the soap server location in the wsdl file. We then, instantiate the PHP SOAP Client object by passing it the relevant parameters. We call the boom() method from the soap server class with two parameters; first->"PHP SOAP", and last->"Tutorial". The WSDL Tutorial, explained how we created these parameters. We accessed the value of the returned complex type, boomResponse, by calling the variable, "return". Note that "return" is a parameter of type, boomResponse. Check the server example for more information. Optionally, you can call __getLastRequest() and __getLastResponse() on the soap client object to see the last SOAP request and response respectively.











Your comments and suggestions are welcome

A Complete PHP SOAP Server Example




In this tutorial, we will look at how to create a complete PHP SOAP server in WSDL mode. You can take a look at How we generated the WSDL file  for this tutorial.




I assumed that you have already enabled SOAP in your PHP configuration.




From the code below, we first create a simple php class, exampleClass with two functions; boom() and getDate(). We also create corresponding classes, boomResponse and getDateResponse for their return type. From the WSDL file, you will notice that the response type for both boom and getDate is defined as complexType. A complexType is represented as Struct in SOAP, which we can represent as a class in php. Also, note that the names of the response classes must match exactly what you have in your WSDL file.




After creating the appropriate classes for our soap server, we instantiate the PHP SoapServer object in WSDL mode by passing the WSDL file as a parameter. We call the setClass() method on the object to set the class for this soap server and finally call the handle() method to handle soap request.





Kindly leave a comment if you find this tutorial useful. 
Related Posts Plugin for WordPress, Blogger...