PHP中soap的用法实例 |
本文标签:PHP,soap,用法 本文实例讲述了PHP中soap的用法,分享给大家供大家参考 。具体用法分析如下: PHP 使用soap有两种方式 。 一、用wsdl文件 服务器端: 复制代码 代码如下: <?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(); ?> 资源描述文件,可以用工具(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> 客户端调用: 复制代码 代码如下: <?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); 希望本文所述对大家的PHP程序设计有所帮助 。 |