php中soap的用法
php 使用soap有两种方式。
一、用wsdl文件
服务器端:
复制代码 代码示例:
<?php
class service
{
public function helloworld()
{
return "hello";
}
public function add($a,$b)
{
return $a+$b;
}
} // www.jb200.com
$server=new soapserver('soap.wsdl',array('soap_version' => soap_1_2));
$server->setclass("service");
$server->handle();
?>
资源描述文件,可以用工具(zend studio)生成。其实就是一个xml文件。
复制代码 代码示例:
<?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>
客户端调用:
复制代码 代码示例:
<?php
$soap = new soapclient('http://localhost/interface/soap.wsdl');
echo $soap->add(1,2);
?>
二、不用wsdl文件
服务器端:
复制代码 代码示例:
<?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();
?>
客户端:
复制代码 代码示例:
<?php
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);
}
?>