Current Location: Home> Latest Articles> How to Create a SOAP API with PHP: A Complete Guide and Practical Tutorial

How to Create a SOAP API with PHP: A Complete Guide and Practical Tutorial

M66 2025-06-11

PHP SOAP API Creation Guide

PHP, as a popular web programming language, can be used to develop server-side applications with SOAP APIs, providing rich functionality and data interaction for clients. This article will introduce how to create a SOAP API with PHP, helping developers quickly grasp the relevant technologies and enhance the functionality of web applications.

1. Determine Required Features and Data

Before creating a SOAP API, it is important to define the features and data that need to be provided. These features and data will directly affect the API interface design. Common service features include data querying, data updating, data deletion, and data addition. When implementing these features, it is important to ensure the accuracy and integrity of the data.

2. Install the SOAP Extension

The PHP SOAP extension is a key component for working with SOAP APIs. Before creating a SOAP API, make sure the SOAP extension is installed and enabled. You can install the SOAP extension in a Linux environment using the following command:

sudo apt-get install php-soap

After installation, modify the php.ini file to ensure that the SOAP extension is enabled. Locate the line “extension=soap.so” in the php.ini file and uncomment it if it is commented out.

3. Create the WSDL File

WSDL (Web Services Description Language) is a standard format used to describe SOAP APIs, containing detailed information about the objects, classes, methods, and parameters offered by the API. To create the WSDL file, you need to write an XML file according to a specific format so that the SOAP protocol can parse and use it. Below is a simple example of a WSDL file:

<definitions name="myapi" targetNamespace="http://www.example.com/soap" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns="http://schemas.xmlsoap.org/wsdl/">
    <message name="getData">
        <part name="id" type="xsd:int"/>
    </message>
    <message name="saveData">
        <part name="data" type="xsd:string"/>
    </message>
    <portType name="myapiPort">
        <operation name="getData">
            <input message="tns:getData"/>
            <output message="tns:getDataResponse"/>
        </operation>
        <operation name="saveData">
            <input message="tns:saveData"/>
            <output message="tns:saveDataResponse"/>
        </operation>
    </portType>
    <binding name="myapiBinding" type="tns:myapiPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="getData">
            <soap:operation soapAction="http://www.example.com/soap#getData"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
        <operation name="saveData">
            <soap:operation soapAction="http://www.example.com/soap#saveData"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="myapi">
        <port name="myapiPort" binding="tns:myapiBinding">
            <soap:address location="http://localhost/myapi/soap"/>
        </port>
    </service>
</definitions>

4. Create the API Code

Before creating the SOAP API, we need to write the implementation code for each API method. Each method requires an implementation class that contains the logic. Below is a simple example of SOAP API implementation:

class MyAPI {
    public function getData($id) {
        // Query data by ID
        $result = mysql_query("SELECT * FROM data WHERE id = $id");
        if ($result) {
            return mysql_fetch_array($result);
        } else {
            return "Query failed";
        }
    }

    public function saveData($data) {
        // Insert or update data
        $sql = "INSERT INTO data (data) VALUES ($data)";
        $result = mysql_query($sql);
        if ($result) {
            return "Data saved successfully";
        } else {
            return "Data save failed";
        }
    }
}

5. Process Requests and Return Responses

When handling SOAP API requests, we need to parse the request data and call the corresponding API methods. Once the request is processed, the response will be returned in the SOAP protocol format. Below is an example of basic SOAP request handling:

try {
    // Parse SOAP request data
    $server = new SoapServer("myapi.wsdl");
    $server->setClass("MyAPI");
    $server->handle();
} catch (Exception $e) {
    echo $e->getMessage();
}

In this example, we use the SoapServer class to create a SOAP server and specify the WSDL file. The SOAP server will use the defined service class to process all requests.

Summary

This article introduced how to create a SOAP API with PHP, including installing the SOAP extension, creating the WSDL file, writing API code, and processing requests and responses. By following these steps, developers can easily build SOAP APIs for web applications and facilitate data interaction and feature expansion. If you encounter any issues, refer to the official PHP documentation or other related resources.