基於wsdl,使用soap實現webservice

    實現webservice的方式多多。這裏我介紹一下使用PHP自帶的soap實現。固然首先要保證在php擴展配置中開啓soap.php

步驟一:修改php.ini。添加extension=php_soap.dll來加載soap內置包。(或者去掉前面的「;」)web

步驟二:寫服務端文件s.php緩存

<?php
/**
 * wsdl服務端  
 */
define('WSDL_URL','hello.wsdl');        //定義WSDL文件路徑
ini_set('soap.wsdl_cache_enabled','0');    //關閉WSDL緩存
 
 //WSDL文件不存在時自動建立
if(!file_exists(WSDL_URL))
{
    require_once 'SoapDiscovery.class.php';
    $disco = new SoapDiscovery('MyTest','www.epetbar.com');//第一參數爲類名,第二個參數是服務的名字能夠隨便寫
    $str = $disco->getWSDL();
    file_put_contents(WSDL_URL,$str);//將生成的wsdl字符串保存到當前目錄的'hello.wsdl'文件中
}
 
//SOAP開啓並接收Client傳入的參數響應 
$server = new SoapServer(WSDL_URL);
$server->setClass('MyTest');//參數爲要加載進去的類名
$server->handle();
 
 
//測試定義公開的類
class MyTest {
        public function fun1(){
            return 'this is fun1()';
        }
        public function fun2($name){
            return 'the name is ' . $name;
        }
} 
?>

     咱們是基於wsdl實現的。因此咱們要生成wsdl文件。目前生成該類型文件有兩種方式。一、使用soapui在zend編輯器中生成。二、使用輔助類SoapDiscovery.class.php來生成。正如代碼所示。咱們選擇的是使用後者。咱們容許s.php這個文件。  WSDL文件不存在時自動建立。而後$server = new SoapServer(WSDL_URL);建立了服務端而且將MyTest類放了進去。編輯器

     SoapDiscovery.class.php文件下載地址(其中包含例子)http://download.csdn.net/detail/wingth11/5641491 測試

生成的hello.wsdl文件以下所示:ui

<?xml version="1.0" ?>
<definitions name="www.epetbar.com" targetNamespace="urn:www.epetbar.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:www.epetbar.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/">
<types xmlns="http://schemas.xmlsoap.org/wsdl/" />
<portType name="www.epetbar.comPort"><operation name="fun1">
<input message="tns:fun1Request" />
<output message="tns:fun1Response" />
</operation>
<operation name="fun2">
<input message="tns:fun2Request" />
<output message="tns:fun2Response" />
</operation>
</portType>
<binding name="www.epetbar.comBinding" type="tns:www.epetbar.comPort">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
<operation name="fun1">
<soap:operation soapAction="urn:www.epetbar.com#MyTest#fun1" />
<input><soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
<operation name="fun2">
<soap:operation soapAction="urn:www.epetbar.com#MyTest#fun2" />
<input><soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="urn:www.epetbar.com" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
<service name="www.epetbar.com">
<documentation />
<port name="www.epetbar.comPort" binding="tns:www.epetbar.comBinding"><soap:address location="http://localhost:80/mycode/webservice/s.php" />
</port>
</service>
<message name="fun1Request">
</message>
<message name="fun1Response">
<part name="fun1" type="xsd:string" />
</message>
<message name="fun2Request">
<part name="name" type="xsd:string" />
</message>
<message name="fun2Response">
<part name="fun2" type="xsd:string" />
</message>
</definitions>

步驟三:寫客戶端文件c.phpthis

<?php
/**
 * wsdl客戶端  
 */
$client = new SoapClient("http://localhost/mycode/webservice/hello.wsdl");
try {
        var_dump($client->__getFunctions());
        $result = $client->fun1();
        var_dump($result);
}
catch (SoapFault $f){
        echo "Error Message: {$f->getMessage()}";
}
?>

     在上面代碼new SoapClient()中的參數是生成的wsdl文件地址。地址中必定要有http://開頭,不然不能實例化成功。運行該文件,結果以下:spa

array (size=2) 0 => string'string fun1()' (length=13) 1 => string'string fun2(string $name)' (length=25)
string'this is fun1()' (length=14)
相關文章
相關標籤/搜索