Home >Backend Development >PHP Tutorial >Examples of soap usage in PHP_PHP tutorial
Usage examples of soap in PHP
There are two ways to use soap in PHP.
1. Use wsdl file
Server side.
{
public function HelloWorld()
{
Return "Hello";
}
public function Add($a,$b)
{
Return $a+$b;
}
}
$server=new SoapServer('soap.wsdl',array('soap_version' => SOAP_1_2));
$server->setClass("service");
$server->handle();
?>
Resource description files can be generated using tools (zend studio). In fact, it is an xml file.
客户端调用
$soap = new SoapClient('http://localhost/interface/soap.wsdl');
echo $soap->Add(1,2);
?>
二、不用wsdl文件
服务器端
{
public function HelloWorld()
{
return "Hello";
}
public function Add($a,$b)
{
return $a+$b;
}
}
$server=new SoapServer(null,array('uri' => "abcd"));
$server->setClass("service");
$server->handle();
?>
客户端
try{
$soap = new SoapClient(null,array(
"location" => "http://localhost/interface/soap.php",
"uri" => "abcd", //资源描述符服务器和客户端必须对应
"style" => SOAP_RPC,
"use" => SOAP_ENCODED
));
echo $soap->Add(1,2);
}catch(Exction $e){
echo print_r($e->getMessage(),true);
}
?>