Current Location: Home> Latest Articles> PHP SOAP Extension Guide: A Complete Tutorial for Building Web Services from Scratch

PHP SOAP Extension Guide: A Complete Tutorial for Building Web Services from Scratch

M66 2025-10-28

PHP SOAP Extension Guide: Building Web Services from Scratch

In modern web development, Web Services play a crucial role in enabling data exchange between systems. SOAP (Simple Object Access Protocol) is an XML-based communication protocol widely used for remote procedure calls across different platforms and programming languages. This tutorial will walk you through how to use the PHP SOAP extension to create and consume Web Services.

Installing and Configuring the SOAP Extension

Before you start coding, make sure your PHP environment has the SOAP extension enabled. Follow these steps to set it up:

  • Open the php.ini configuration file located in your PHP installation directory.
  • Find the following line and remove the leading semicolon to enable the SOAP extension:
;extension=php_soap.dll
  • Save the file and restart your web server to apply the changes.

Once these steps are complete, the SOAP extension should be ready to use.

Creating a SOAP Web Service

Let’s create a simple PHP SOAP server. Create a new file named soap_server.php and add the following code:

<?php
class HelloWorld {
    public function sayHello() {
        return "Hello, World!";
    }
}

$options = array(
    'uri' => 'http://localhost/soap_server.php'
);

$server = new SoapServer(null, $options);
$server->setClass('HelloWorld');
$server->handle();
?>

In this example:

  • The HelloWorld class contains a public method sayHello() that returns a simple greeting.
  • The SoapServer class is used to create the SOAP server with a defined URI.
  • The setClass() method binds the class to the server, and handle() processes incoming SOAP requests.

Creating a SOAP Client

Now, let’s create a SOAP client to call our service. Create a new file named soap_client.php and insert the following code:

<?php
$options = array(
    'uri' => 'http://localhost/soap_server.php',
    'location' => 'http://localhost/soap_server.php'
);

$client = new SoapClient(null, $options);
$result = $client->sayHello();

echo $result;
?>

Here’s what happens in this script:

  • The SoapClient class is used to create a client instance.
  • The uri and location parameters define where the client connects to the server.
  • The sayHello() method calls the server and returns the result.

Running and Testing the Web Service

Place both soap_server.php and soap_client.php in your web server’s root directory. Then:

  • Visit http://localhost/soap_server.php in your browser to check if the SOAP server is running properly.
  • Visit http://localhost/soap_client.php to test the client — it should display Hello, World!.

If you see the expected output, your SOAP server and client are communicating successfully.

Conclusion

By following these steps, you’ve learned how to create a basic PHP SOAP Web Service. From setting up the environment to building and testing both the server and client, SOAP provides a reliable and efficient way to enable remote communication in PHP applications. With this foundation, you can expand and customize your own Web Services to suit more complex business needs.