您的位置:首页 > 其它

利用SOAP协议开发web service

2010-06-01 15:33 218 查看
web service 使用XML格式为第三方应用程序提供API的一种技术;

SOAP是简单对像存储协议

建立web service步骤如下:

1、建立wsdl文件,我用zend studio建的,视图如下



下面是视图对应的源文件,可直接COPY。

<?xml version="1.0" encoding="UTF-8"?>

<wsdl:definitions

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

xmlns:tns="http://www.example.org/mytest/"

xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

name="mytest1"

targetNamespace="http://www.example.org/mytest/">

<wsdl:types><xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/mytest/"><xsd:complexType name="int"></xsd:complexType></xsd:schema></wsdl:types>

<wsdl:message name="getDataRequest
">

<wsdl:part name="param1" type="xsd:string"/>//可以接收更多的输入参数

<wsdl:part name="param2" type="xsd:string"/>

</wsdl:message>

<wsdl:message name="getDataResponse
">

<wsdl:part name="getDataResponse" type="xsd:string"/>

</wsdl:message>

<wsdl:portType name="ISoapDemo">

<wsdl:operation name="getData
">//服务端所写的参数

<wsdl:input message="tns:getDataRequest
"/>

<wsdl:output message="tns:getDataResponse
"/>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="mytestSOAP" type="tns:ISoapDemo">

<soap:binding style="rpc
" transport="http://schemas.xmlsoap.org/soap/http"/>//使用rpc协议

<wsdl:operation name="getData
">

<soap:operation soapAction="http://www.example.org/mytest/getData"/>

<wsdl:input>

<soap:body namespace="http://www.example.org/mytest/" use="literal"/>

</wsdl:input>

<wsdl:output>

<soap:body namespace="http://www.example.org/mytest/" use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="soapDemo">

<wsdl:port binding="tns:mytestSOAP" name="portName">

<soap:address location="http://test.com/book/server.php
"/>//写自己的请求地址

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

2、服务端文件server.php

<?php

ini_set("soap.wsdl_cache_enabled", "0");

function getData($param1,$param2){

$rs=$param1.'=>'.$param2;

return $rs;

}

$server = new SoapServer("mytest.wsdl");

$server->addFunction("getData");

if (isset($HTTP_RAW_POST_DATA)) {

$request = $HTTP_RAW_POST_DATA;

} else {

$request = file_get_contents('php://input');

}

$server->handle($request);

?>

3、写一个客户端来测试test.php

<?php

ini_set("soap.wsdl_cache_enabled", "0");

$client = new SoapClient("http://test.com/mytest.wsdl");

$return = $client->getData('key','val');

print_r($return);

?>

还有一个重要的是PHP必需开启SOAP,在PHP.INI中加入以下配置

extension=php_soap.dll

[soap]

soap.wsdl_cache_enabled=0

soap.wsdl_cache_dir="c:/window/temp"

soap.wsdl_cache_ttl=86400

完成,运行http://test.com/test.php就可以显示key=>val
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: