Last active
June 20, 2024 13:28
-
-
Save ZiTAL/ca47dacd93e36495e9ec10295dd2445d to your computer and use it in GitHub Desktop.
SOAP: php simple example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run server:
Exec client:
Result