{"id":12518,"date":"2016-05-20T16:15:41","date_gmt":"2016-05-20T13:15:41","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=12518"},"modified":"2018-01-09T10:57:49","modified_gmt":"2018-01-09T08:57:49","slug":"php-soapclient-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/","title":{"rendered":"PHP SoapClient Example"},"content":{"rendered":"<p>In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is a technology that uses a protocol (SOAP in this example) for exchanging data between applications over the network.<\/p>\n<p>If we are developing a client for consuming a web service, we have to look at the protocol that the service is using. This example is for looking at the resources provided by PHP for developing a client for a web service implementing the SOAP protocol.<\/p>\n<p>For this example, we will use:<\/p>\n<ul>\n<li>Ubuntu (14.04) as Operating System.<\/li>\n<li>Apache HTTP server (2.4.7).<\/li>\n<li>PHP (5.5.9).<\/li>\n<\/ul>\n<p>[ulp id=&#8217;8njY7i2QRy6sg8pg&#8217;]<\/p>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip environment preparation creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<h2>1. Preparing the environment<\/h2>\n<h3>1.1. Installation<\/h3>\n<p>Below, commands to install Apache and PHP are shown:<\/p>\n<pre class=\"brush:bash\">sudo apt-get update\r\nsudo apt-get install apache2 php5 libapache2-mod-php5\r\nsudo service apache2 restart<\/pre>\n<h3>1.2. PHP configuration<\/h3>\n<p>Even if it&#8217;s not necessary, is recommendable to disable the SOAP WSDL cache for development environments. In <code>\/etc\/php5\/apache2\/php.ini<\/code> file, set the <code>soap.wsdl_cache_enabled<\/code> directive to <code>0<\/code>:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>php.ini<\/em><\/span><\/p>\n<pre class=\"brush:bash\">soap.wsdl_cache_enabled=0<\/pre>\n<p>Don&#8217;t forget to restart Apache after doing any change.<\/p>\n<p><strong>Note:<\/strong> if you want to use Windows, installing XAMPP is the fastest and easiest way to install a complete web server that meets the prerequisites.<\/p>\n<h2><span id=\"code\">2. What is SOAP?<\/span><\/h2>\n<p>SOAP (Simple Object Access Protocol) is a standard protocol that defines how two different objects in different processes can communicate each other, through data exchange in XML format. Is one of the widest protocols used in web services.<\/p>\n<p>In other words, this protocol allows to call method of objects that are defined in remote machines.<\/p>\n<h2>3. PHP example of SOAP clients<\/h2>\n<p>The server offering their SOAP services can define them in two different ways:<\/p>\n<ul>\n<li>With WSDL (Web Services Definition Language)<\/li>\n<li>Without WSDL<\/li>\n<\/ul>\n<p>From the client point of view, there are very few differences, but let&#8217;s see how to proceed for each one.<\/p>\n<p>We will consider the following scenario, which is quite typical: a web service that inserts and reads from a database (simulated with a plain text file), for which we will develop a client.<\/p>\n<p>Is responsibility of the server offering the service to let know the protocol used, as same as the available method definitions, in order to let the clients know how to deal with the service.<\/p>\n<p>Even if this tutorial is about the client itself, we will see also the server side, in order to test that the client is actually working.<\/p>\n<h3>3.1. Working directory structure<\/h3>\n<p>For this example, we will use the following structure:<\/p>\n<pre class=\"brush:bash\">php_soapclient_example\/\r\n\u251c\u2500\u2500 simple_soap_client_class.php\r\n\u251c\u2500\u2500 simple_soap_server_class.php\r\n\u251c\u2500\u2500 handle_soap_request.php\r\n\u251c\u2500\u2500 no_wsdl\r\n\u2502\u00a0\u00a0 \u2514\u2500\u2500 server_endpoint.php\r\n\u2514\u2500\u2500 wsdl\r\n\u00a0\u00a0\u00a0 \u251c\u2500\u2500 server_endpoint.php\r\n\u00a0\u00a0\u00a0 \u2514\u2500\u2500 simple_service_definition.wsdl\r\n\r\n<\/pre>\n<p>Where the root directory, <code>php_soapclient_example<\/code>, will be in the web server&#8217;s root directory, which is <code>\/var\/www\/html\/<\/code> by default.<\/p>\n<p>Briefly explained each file:<\/p>\n<ul>\n<li><code>simple_soap_client_class.php<\/code> is the class defined for the SOAP client methods.<\/li>\n<li><code>simple_soap_server_class.php<\/code> is the class defined for the SOAP server methods.<\/li>\n<li><code>handle_soap_request.php<\/code> is for instantiate and use the <code>SimpleSoapClient<\/code> class defined in <code>simple_soap_client_class.php<\/code>.<\/li>\n<li>In the directories <code>wsdl<\/code> and <code>no_wsdl<\/code>, the service defined in <code>simple_soap_server_class.php<\/code> is offered, in the way it accords to each of the modes. We won&#8217;t go into details for these, since it is not the purpose of this example. The only thing we have to know is that in one of the directories there is a service offered in WSDL mode, and that in the other, in no WSDL mode; they are two independent web services that use the code defined in <code>simple_soap_server_class.php<\/code>.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> the WSDL mode needs a <code>.wsdl<\/code> file, where the web service is defined. We are not going to see it in this example, since the aim is not to see how to build a WSDL definition file, but it&#8217;s also available to download in the last section of the example.<\/p>\n<h3>3.2. The server<\/h3>\n<p>As said, our server will read and write data into a text file. Let&#8217;s see it:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>simple_soap_server_class.php<\/em><\/span><\/p>\n<pre class=\"brush:php; wrap-lines:false; highlight:[16,33]\">&lt;?php\r\n\r\n\/**\r\n\u00a0* Methods of our simple SOAP service.\r\n\u00a0*\/\r\nclass SimpleSoapServer {\r\n\r\n\u00a0\u00a0\u00a0 const FILENAME = 'data.txt';\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Inserts data. Invoked remotely from SOAP client.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param $data Data to insert.\r\n\u00a0\u00a0\u00a0\u00a0 * @return Resume of operation.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function insertData($data) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $writtenBytes = file_put_contents(self::FILENAME, $data . '&lt;br&gt;', FILE_APPEND);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if ($writtenBytes) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $response = \"$writtenBytes bytes have been inserted.\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $response = 'Error inserted data.';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return $response;\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Reads data. Invoked remotely from SOAP client.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @return Data of file.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function readData() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $contents = file_get_contents(self::FILENAME);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return $contents;\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>So simple, about reading and writing into a text file.<\/p>\n<p>Note the functions this server implements, in lines 16 and 33. This functions will be those invoked by the client.<\/p>\n<h3>3.3. The client<\/h3>\n<p>As we said above, depending of the mode (WSDL or not WSDL), the client has to handle the connection in a different way but, once stablished, the procedure is the same. So, let&#8217;s code a class that works for both modes, handling the SoapClient class instantiation accordingly to the mode:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>simple_soap_client_class.php<\/em><\/span><\/p>\n<pre class=\"brush:php; wrap-lines:false; highlight:[35,45,62,73]\">&lt;?php\r\n\r\n\/**\r\n\u00a0* Methods for dealing with SOAP service.\r\n\u00a0*\/\r\nclass SimpleSoapClient {\r\n\r\n\u00a0\u00a0\u00a0 const MODE_WSDL = 'wsdl';\r\n\u00a0\u00a0\u00a0 const MODE_NO_WSDL = 'no_wsdl';\r\n\r\n\u00a0\u00a0\u00a0 private $client;\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * SimpleSoapClient class constructor.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param $soapMode The SOAP mode, WSDL or non-WSDL.\r\n\u00a0\u00a0\u00a0\u00a0 * @param $serverLocation The location of server.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function __construct($soapMode, $serverLocation) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;initializeClient($soapMode, $serverLocation);\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Instantiates the SoapClient, depending on the specified mode.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * For WSDL, it just has to be instantiated with the location of the service, which actually has to be the\r\n\u00a0\u00a0\u00a0\u00a0 * .wsdl location.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * For non-WSDL, the first parameter of the constructor has to be null; and the second, an array specifying\r\n\u00a0\u00a0\u00a0\u00a0 * both location and URI (which can be the same, the important parameter is the location).\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 protected function initializeClient($soapMode, $serverLocation) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 switch ($soapMode) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case self::MODE_WSDL:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;client = new SoapClient($serverLocation);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 case self::MODE_NO_WSDL:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $options = array(\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'location' =&gt; $serverLocation,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 'uri' =&gt; $serverLocation\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 );\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $this-&gt;client = new SoapClient(NULL, $options);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 default:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 throw new Exception('Error: invalid SOAP mode provided.');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Inserts data remotely into the SOAP service.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @param $data Data to insert remotely.\r\n\u00a0\u00a0\u00a0\u00a0 * @return Response from remote service.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function insertData($data) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $response = $this-&gt;client-&gt;insertData($data);\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return $response;\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 \/**\r\n\u00a0\u00a0\u00a0\u00a0 * Reads data from SOAP service.\r\n\u00a0\u00a0\u00a0\u00a0 *\r\n\u00a0\u00a0\u00a0\u00a0 * @return Data received from remote service.\r\n\u00a0\u00a0\u00a0\u00a0 *\/\r\n\u00a0\u00a0\u00a0 public function readData() {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return $this-&gt;client-&gt;readData();\r\n\u00a0\u00a0\u00a0 }\r\n}<\/pre>\n<p>The lines 35 and 45 instantiate the <code>SoapClient<\/code> class, depending on the <code>$mode<\/code> received. This is how it works for each mode:<\/p>\n<ul>\n<li>For WSDL mode, we have just to pass the server location to the constructor. For this mode, the <strong>location must be the WSDL definition file (<code>.wsdl<\/code>), not a PHP file<\/strong>.<\/li>\n<li>For non WSDL mode, the first parameter has to be null (because we are not accessing a WSDL definition). So, the location must be defined in a different way, providing an array with <code>'location'<\/code> and <code>'uri'<\/code> elements, with the server location as values. In this case, the <strong>location must be the PHP file handling the web service<\/strong>.<\/li>\n<\/ul>\n<p>After the instantiation, the communication with the service is pretty simple. We have just to call the methods that we saw defined in <code>SimpleSoapServer<\/code> class, in <code>simple_soap_client_class.php<\/code>, through the <code>SoapClient<\/code> class instance. As we said before, we are calling methods that are defined in other place. What PHP <code>SoapClient<\/code> does, is to provide us those methods defined by the web service, and, when we call them, it will execute them in the server through the SOAP protocol that it has already implemented, with no needing to care about how it works. Seems magic, doesn&#8217;t it?<\/p>\n<p>If you don&#8217;t believe it, let&#8217;s see a script to use this client.<\/p>\n<h3>3.4. Using the client<\/h3>\n<p>The following script allows us to use the client to communicate with the service, through GET parameters. These are the parameters available:<\/p>\n<ul>\n<li><code>'mode'<\/code>, to specify the mode (WSDL or non WSDL).<\/li>\n<li><code>'action'<\/code>, to specify the action to perform. Available values are <code>'insert'<\/code> and <code>'read'<\/code>.<\/li>\n<li><code>'value'<\/code>, to specify the value to insert, only necessary when the action is <code>'insert'<\/code>.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><em>handle_soap_request.php<\/em><\/span><\/p>\n<pre class=\"brush:php; wrap-lines:false\">&lt;?php\r\n\r\nrequire_once('simple_soap_client_class.php');\r\n\r\n\/\/ GET parameter definition.\r\ndefine('ACTION_INSERT', 'insert');\r\ndefine('ACTION_READ', 'read');\r\ndefine('INSERT_VALUE', 'value');\r\ndefine('MODE_WSDL', 'wsdl');\r\ndefine('MODE_NO_WSDL', 'no_wsdl');\r\n\r\n\/\/ Server location definition.\r\ndefine('LOCATION_WSDL', 'http:\/\/127.0.0.1\/php_soapclient_example\/wsdl\/simple_service_definition.wsdl');\r\ndefine('LOCATION_NO_WSDL', 'http:\/\/127.0.0.1\/php_soapclient_example\/wsdl\/server_endpoint.php');\r\n\r\n\/\/ Function definitions.\r\n\r\n\/**\r\n\u00a0* Checks if the given parameters are set. If one of the specified parameters is not set,\r\n\u00a0* die() is called.\r\n\u00a0*\r\n\u00a0* @param $parameters The parameters to check.\r\n\u00a0*\/\r\nfunction checkGETParametersOrDie($parameters) {\r\n\u00a0\u00a0\u00a0 foreach ($parameters as $parameter) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 isset($_GET[$parameter]) || die(\"Please, provide '$parameter' parameter.\");\r\n\u00a0\u00a0\u00a0 }\r\n}\r\n\r\n\/**\r\n\u00a0* Instantiates the SOAP client, setting the server location, depending on the mode.\r\n\u00a0* If any error occurs, the page will die.\r\n\u00a0*\r\n\u00a0* @param $mode The SOAP mode, 'wsdl' or 'no_wsdl'.\r\n\u00a0* @return SoapClient class instance.\r\n\u00a0*\/\r\nfunction instantiateSoapClient($mode) {\r\n\u00a0\u00a0\u00a0 if ($mode == MODE_WSDL) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $serverLocation = LOCATION_WSDL;\r\n\u00a0\u00a0\u00a0 } else {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $serverLocation = LOCATION_NO_WSDL;\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $soapClient = new SimpleSoapClient($mode, $serverLocation);\r\n\u00a0\u00a0\u00a0 } catch (Exception $exception) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 die('Error initializing SOAP client: ' . $exception-&gt;getMessage());\r\n\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0 return $soapClient;\r\n}\r\n\r\n\/\/ Flow starts here.\r\n\r\ncheckGETParametersOrDie(['mode', 'action']);\r\n\r\n$mode = $_GET['mode'];\r\n$action = $_GET['action'];\r\n\r\n$soapClient = instantiateSoapClient($mode);\r\n\r\nswitch($action) {\r\n\u00a0\u00a0\u00a0 case ACTION_INSERT:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 checkGETParametersOrDie([INSERT_VALUE]);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $value = $_GET[INSERT_VALUE];\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $response = $soapClient-&gt;insertData($value);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo \"Response from SOAP service: $response&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } catch (Exception $exception) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 die('Error inserting into SOAP service: ' . $exception-&gt;getMessage());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\r\n\u00a0\u00a0\u00a0 case ACTION_READ:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 $data = $soapClient-&gt;readData();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo \"Received data from SOAP service:&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 echo $data;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 } catch (Exception $exception) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 die('Error reading from SOAP service: ' . $exception-&gt;getMessage());\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n\r\n\u00a0\u00a0\u00a0 default:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 die('Invalid \"action\" specified.');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 break;\r\n}<\/pre>\n<p>Let&#8217;s try it.<\/p>\n<p>If we enter in the browser <code>http:\/\/127.0.0.1\/php_soapclient_example\/handle_soap_request.php?mode=no_wsdl&amp;action=insert&amp;value=testing_no_wsdl<\/code>, the service will create a <code>no_wsdl\/data.txt<\/code> file (if not exists already), writing the provided value <code>'testing_no_wsdl'<\/code>, and the following will be printed:<\/p>\n<blockquote><p>Response from SOAP service: 19 bytes have been inserted.<\/p><\/blockquote>\n<p>(The 4 bytes extra correspond to additional <code>&lt;br&gt;<\/code> characters inserted into the file).<\/p>\n<p>The service offers a method to reading all the data, so, if we enter in the browser <code>http:\/\/127.0.0.1\/php_soapclient_example\/handle_soap_request.php?mode=no_wsdl&amp;action=read<\/code>, the following will be printed:<\/p>\n<blockquote><p>Received data from SOAP service:<br \/>\ntesting_no_wsdl<\/p><\/blockquote>\n<p>We can check it also for WSDL mode, setting <code>mode=wsdl<\/code> in the parameters.<\/p>\n<h2>4. Considerations<\/h2>\n<p>As said in section 3, when we are developing a web service client (no matter if SOAP, REST, etc.), the service provider has to provide also documentation about the available methods; we need to know the definition of these: which parameters is expecting, af if it returns something. Because of that, the most usual way to develop a SOAP web service is using WSDL files, to provide documentation of the available methods. In any case, the <code>SoapClient<\/code> has a method to get the available methods, for debugging purposes, named <code>__getFunctions()<\/code>. So, we could see the offered methods by the service with the following:<\/p>\n<pre class=\"brush:php\">var_dump($soapClient-&gt;__getFunctions())<\/pre>\n<p>Assuming that <code>$soapClient<\/code> has been instanced properly.<\/p>\n<p>Also note that, if the service does not offer an encrypted connection, the communication between the client and the server will be made in plain text. So, if we have to develop a client that has to deal with sensible information, we should ensure that the communication with the server can be considered safe.<\/p>\n<h2>5. Summary<\/h2>\n<p>We have seen how to develop a client for a SOAP web service using PHP&#8217;s SoapClient class. The server may offer the service using WSDL, or not, something to take into account when we are instantiating the <code>SoapClient<\/code> class. Then, to use the methods provided by the service, we just have to call those methods in <code>SoapClient<\/code> instance, as they were ours.<\/p>\n<h2>6. Download the source code<\/h2>\n<p>This was an example of SoapClient with PHP.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2016\/05\/PHPSoapClientExample.zip\"><strong>PHPSoapClientExample<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is a technology that uses a protocol (SOAP in this example) for exchanging data between applications over the network. If we are developing a client for consuming &hellip;<\/p>\n","protected":false},"author":160,"featured_media":930,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[371,370],"class_list":["post-12518","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-soap","tag-web-services"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PHP SoapClient Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP SoapClient Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-20T13:15:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T08:57:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Toni\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Toni\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/\"},\"author\":{\"name\":\"Toni\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\"},\"headline\":\"PHP SoapClient Example\",\"datePublished\":\"2016-05-20T13:15:41+00:00\",\"dateModified\":\"2018-01-09T08:57:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/\"},\"wordCount\":1315,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"keywords\":[\"SOAP\",\"web services\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/\",\"name\":\"PHP SoapClient Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"datePublished\":\"2016-05-20T13:15:41+00:00\",\"dateModified\":\"2018-01-09T08:57:49+00:00\",\"description\":\"In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/php\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP SoapClient Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966\",\"name\":\"Toni\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g\",\"caption\":\"Toni\"},\"url\":\"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP SoapClient Example - Web Code Geeks - 2026","description":"In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/","og_locale":"en_US","og_type":"article","og_title":"PHP SoapClient Example - Web Code Geeks - 2026","og_description":"In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is","og_url":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2016-05-20T13:15:41+00:00","article_modified_time":"2018-01-09T08:57:49+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","type":"image\/jpeg"}],"author":"Toni","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Toni","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/"},"author":{"name":"Toni","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966"},"headline":"PHP SoapClient Example","datePublished":"2016-05-20T13:15:41+00:00","dateModified":"2018-01-09T08:57:49+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/"},"wordCount":1315,"commentCount":1,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","keywords":["SOAP","web services"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/","url":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/","name":"PHP SoapClient Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","datePublished":"2016-05-20T13:15:41+00:00","dateModified":"2018-01-09T08:57:49+00:00","description":"In this example we will see an example of a SOAP client communicating with a server. SOAP is one of many web service protocol definition. A web service is","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/php-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/php\/php-soapclient-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"PHP","item":"https:\/\/www.webcodegeeks.com\/category\/php\/"},{"@type":"ListItem","position":3,"name":"PHP SoapClient Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/54a7be647b0b871cff41cbf3d2a95966","name":"Toni","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/21c1661474c78b4757355b8beef9ab1d14f490ee3a3e67392f4e618d36643d4c?s=96&d=mm&r=g","caption":"Toni"},"url":"https:\/\/www.webcodegeeks.com\/author\/julen-pardo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/160"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=12518"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/12518\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/930"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=12518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=12518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=12518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}