Usage Examples of soap in PHP

  • 2021-07-22 09:15:11
  • OfStack

This article describes the use of soap in PHP, and shares it with you for your reference. The specific usage analysis is as follows:

PHP uses soap in two ways.

1. Use the wsdl file

Server side:

<?php
class service
{
  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 file, which can be generated by tool (zend studio). It's actually an xml file.
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/interface/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="soap" targetNamespace="http://localhost/interface/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost/interface/">
      <xsd:element name="HelloWorld">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="in" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="HelloWorldResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="out" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="Add">
       <xsd:complexType>
        <xsd:sequence>
         <xsd:element name="in" type="xsd:int"></xsd:element>
        </xsd:sequence>
       </xsd:complexType>
      </xsd:element>
      <xsd:element name="AddResponse">
       <xsd:complexType>
        <xsd:sequence>          <xsd:element name="out" type="xsd:int"></xsd:element>
        </xsd:sequence>
       </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
   <wsdl:message name="AddRequest">    <wsdl:part name="a" type="xsd:int"></wsdl:part>
   <wsdl:part name="b" type="xsd:int"></wsdl:part>
  </wsdl:message>
  <wsdl:message name="AddResponse">
   <wsdl:part name="c" type="xsd:int"></wsdl:part>
  </wsdl:message>
  <wsdl:portType name="TestSoap">     <wsdl:operation name="Add">
     <wsdl:input message="tns:AddRequest"></wsdl:input>
     <wsdl:output message="tns:AddResponse"></wsdl:output>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="soapSOAP" type="tns:TestSoap">
   <soap:binding style="document"
    transport="http://schemas.xmlsoap.org/soap/http" />
   <wsdl:operation name="Add">
    <soap:operation soapAction="http://localhost/interface/Add" />
    <wsdl:input>
     <soap:body use="literal"
      namespace="http://localhost/interface/" />
    </wsdl:input>
    <wsdl:output>
     <soap:body use="literal"
      namespace="http://localhost/interface/" />
    </wsdl:output>
   </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="TestSoap">
    <wsdl:port binding="tns:soapSOAP" name="soapSOAP">
      <soap:address location="http://localhost/interface/myservice.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

Client invocation:
<?php
$soap = new SoapClient('http://localhost/interface/soap.wsdl');
echo $soap->Add(1,2);
?>

2. Don't use wsdl files

Server side:

<?php
class service
{
  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();
?>

Client:
<?php
try{
 $soap = new SoapClient(null,array(
   "location" => "http://localhost/interface/soap.php",
   "uri"      => "abcd",  // Resource descriptor server and client must correspond
   "style"    => SOAP_RPC,
   "use"      => SOAP_ENCODED
      ));  echo $soap->Add(1,2);
}catch(Exction $e){
 echo print_r($e->getMessage(),true);
}
?>

I hope this article is helpful to everyone's PHP programming.


Related articles: