Skip to content

Instantly share code, notes, and snippets.

@ZiTAL
Last active June 20, 2024 13:28
Show Gist options
  • Save ZiTAL/ca47dacd93e36495e9ec10295dd2445d to your computer and use it in GitHub Desktop.
Save ZiTAL/ca47dacd93e36495e9ec10295dd2445d to your computer and use it in GitHub Desktop.
SOAP: php simple example
<?php
// Disable WSDL caching
ini_set("soap.wsdl_cache_enabled", "0");
$wsdl = "http://localhost:8000/service.wsdl";
try
{
$a = 4;
$b = 5;
$client = new SoapClient($wsdl);
$result = $client->addNumbers($a, $b);
echo "The result of adding {$a} and {$b} is: {$result}\n";
}
catch (Exception $e)
{
echo "Error: " . $e->getMessage()."\n";
}
<?php
// Disable WSDL caching
ini_set("soap.wsdl_cache_enabled", "0");
// Define the function that will be available via SOAP
function addNumbers($a, $b)
{
return $a + $b;
}
// Define the server class
class MySoapServer
{
public function addNumbers($a, $b)
{
return addNumbers($a, $b);
}
}
$server = new SoapServer(null, array('uri' => "http://localhost:8000"));
$server->setClass('MySoapServer');
$server->handle();
<?xml version="1.0" encoding="UTF-8"?>
<definitions name="MySoapService"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<message name="addNumbersRequest">
<part name="a" type="xsd:int"/>
<part name="b" type="xsd:int"/>
</message>
<message name="addNumbersResponse">
<part name="result" type="xsd:int"/>
</message>
<portType name="MySoapPortType">
<operation name="addNumbers">
<input message="tns:addNumbersRequest"/>
<output message="tns:addNumbersResponse"/>
</operation>
</portType>
<binding name="MySoapBinding" type="tns:MySoapPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="addNumbers">
<input>
<soap:body use="literal" />
</input>
<output>
<soap:body use="literal" />
</output>
</operation>
</binding>
<service name="MySoapService">
<port name="MySoapPort" binding="tns:MySoapBinding">
<soap:address location="http://localhost:8000"/>
</port>
</service>
</definitions>
@ZiTAL
Copy link
Author

ZiTAL commented Jun 4, 2024

Run server:

php -S localhost:8000

Exec client:

php client.php

Result

The result of adding 4 and 5 is: 9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment