wsdlに基づいてsoapを用いてwebserviceを実現


Webサービスを実現する方法は多い.ここではPHPが持参したsoapを使った実装を紹介します.もちろんphp拡張構成でsoapをオンにすることを保証する.
ステップ1:phpを修正する.ini.extension=php_を追加soap.dllはsoap内蔵パッケージをロードします.(または前の「;」)
ステップ2:サービス側ファイル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ファイルを生成します.現在、このタイプのファイルを生成するには2つの方法があります.1、soapuiを使用してzendエディタで生成します.2、補助類を使用するSoapDiscovery.class.phpを生成します.コードに示すように.私たちが選んだのは後者を使うことです.s.phpというファイルを許可します.WSDLファイルが存在しない場合に自動的に作成されます.そして$server=new SoapServer(WSDL_URL);サービス側を作成し、MyTestクラスを入れました.
     SoapDiscovery.class.phpファイルダウンロードアドレス(例を含む)http://download.csdn.net/detail/wingth11/5641491
生成されたhello.wsdlファイルは次のようになります.
<?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>

ステップ3:クライアントファイルc.phpを書く
<?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://先頭が必要です.そうしないとインスタンス化できません.ファイルを実行すると、次の結果が得られます.
array (size=2) 0 => string'string fun1()' (length=13) 1 => string'string fun2(string $name)' (length=25)
string'this is fun1()' (length=14)