How to call a SOAP Web Service from PHP?

In this article we will show to to invoke a JAX-WS Web Service from PHP using the SOAP Client PHP Class.

Coding the Server Side Web Service

Firstly, we will build a sample SOAP Web Service and deploy in on WildFly application Server:

package com.sample;

import javax.jws.WebMethod;    
import javax.jws.WebService;   
import javax.jws.soap.SOAPBinding;   

   
  
@WebService(targetNamespace = "https://www.mastertheboss.com/", serviceName = "HelloWorldService") 
@SOAPBinding(style = SOAPBinding.Style.RPC)   
  
public class HelloWorld     
{   
   @WebMethod  
   public String sayHello(String input)   
   {   
      return "Welcome " +input + " !";   
   }   
             
      
}

Deploy the Web Service. This will expose the following WSDL:

<definitions name="HelloWorldService" targetNamespace="https://www.mastertheboss.com/" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="https://www.mastertheboss.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <types /> 
  <message name="HelloWorld_sayHello">
   <part name="arg0" type="xsd:string" /> 
  </message>
 <message name="HelloWorld_sayHelloResponse">
  <part name="return" type="xsd:string" /> 
 </message>
 <portType name="HelloWorld">
 <operation name="sayHello" parameterOrder="arg0">
  <input message="tns:HelloWorld_sayHello" /> 
  <output message="tns:HelloWorld_sayHelloResponse" /> 
 </operation>
 </portType>
 <binding name="HelloWorldBinding" type="tns:HelloWorld">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
 <operation name="sayHello">
  <soap:operation soapAction="" /> 
 <input>
  <soap:body namespace="https://www.mastertheboss.com/" use="literal" /> 
  </input>
 <output>
  <soap:body namespace="https://www.mastertheboss.com/" use="literal" /> 
  </output>
  </operation>
  </binding>
  <service name="HelloWorldService">
   <port binding="tns:HelloWorldBinding" name="HelloWorldPort">
     <soap:address location="http://127.0.0.1:8080/HelloWorld/helloWorldService" /> 
   </port>
  </service>
</definitions>

Creating the PHP Client

To call a JAX-WS web service from PHP, you can use the PHP SoapClient class. Here is an example of how you can use the SoapClient class to call a JAX-WS web service:

$wsdl = "http://127.0.0.1:8080/HelloWorld/helloWorldService?wsdl";
$client = new SoapClient($wsdl);

// Call a web service function with parameters
$response = $client->sayHello(array("name" => "Frank"));

// Print the response
print_r($response);

Save this php page in your htdocs Apache folder and verify that the it correctly invokes the JBoss Web Service:

http://localhost/soapclient.php

Welcome Frank !

You may also need to set additional options for the SoapClient object, such as the authentication credentials for the web service, or the location of the web service endpoint. You can set these options using the SoapClient’s __setOptions method. For example:

$options = array(
    'login' => 'username',
    'password' => 'password',
    'location' => 'http://example.com/service/endpoint'
);

$client->__setOptions($options);

That’s all! I hope this explains how to invoke a JAX-WS Web service from PHP.