PHP中soap的用法實(shí)例
本文實(shí)例講述了PHP中soap的用法,分享給大家供大家參考。具體用法分析如下:
PHP 使用soap有兩種方式。
一、用wsdl文件
服務(wù)器端:
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();
?>
資源描述文件,可以用工具(zend studio)生成。其實(shí)就是一個(gè)xml文件。
<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>
客戶端調(diào)用:
$soap = new SoapClient('http://localhost/interface/soap.wsdl');
echo $soap->Add(1,2);
?>
二、不用wsdl文件
服務(wù)器端:
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();
?>
客戶端:
try{
$soap = new SoapClient(null,array(
"location" => "http://localhost/interface/soap.php",
"uri" => "abcd", //資源描述符服務(wù)器和客戶端必須對(duì)應(yīng)
"style" => SOAP_RPC,
"use" => SOAP_ENCODED
));
echo $soap->Add(1,2);
}catch(Exction $e){
echo print_r($e->getMessage(),true);
}
?>
希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。
- PHP程序員簡(jiǎn)單的開展服務(wù)治理架構(gòu)操作詳解(二)
- PHP程序員簡(jiǎn)單的開展服務(wù)治理架構(gòu)操作詳解(一)
- PHP中soap用法示例【SoapServer服務(wù)端與SoapClient客戶端編寫】
- php中curl和soap方式請(qǐng)求服務(wù)超時(shí)問題的解決
- PHP使用NuSOAP調(diào)用Web服務(wù)的方法
- PHP使用SOAP擴(kuò)展實(shí)現(xiàn)WebService的方法
- PHP使用SOAP調(diào)用.net的WebService數(shù)據(jù)
- PHP Class SoapClient not found解決方法
- PHP實(shí)現(xiàn)Soap通訊的方法
- PHP使用SOAP調(diào)用API操作示例
- PHP程序員簡(jiǎn)單的開展服務(wù)治理架構(gòu)操作詳解(三)
相關(guān)文章
javascript數(shù)組與php數(shù)組的地址傳遞及值傳遞用法實(shí)例
這篇文章主要介紹了javascript數(shù)組與php數(shù)組的地址傳遞及值傳遞用法,實(shí)例分析了javascript與php的數(shù)組使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01php switch語句多個(gè)值匹配同一代碼塊應(yīng)用示例
switch 語句一行接一行地執(zhí)行(實(shí)際上是語句接語句)。開始時(shí)沒有代碼被執(zhí)行,下面有個(gè)示例,需要的朋友可以參考下2014-07-07學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)橋梁模式(bridge)
這篇文章主要介紹了php設(shè)計(jì)模式中的橋梁模式,使用php實(shí)現(xiàn)橋梁模式,感興趣的小伙伴們可以參考一下2015-12-12Linux下CoreSeek及PHP擴(kuò)展模塊的安裝
前提條件是系統(tǒng)己安裝完成apache mysql php的WEB服務(wù)。我是以yum來安裝的。如果你沒有安裝過請(qǐng)按照下面給出的鏈接先完成基本的LAMP環(huán)境的安裝2012-09-09