基于PHP的Web Service开发入门教程

jopen 12年前
     <p>一个php做Web Service开发入门实例,代码如下:</p>    <p>addclass.php 这个是要调用的类文件,就是处理数据的核心<br /> </p>    <pre class="brush:php; toolbar: true; auto-links: false;"><?php class addclass{ function add($x,$y){ return $x+$y; } } ?></pre>    <p></p>    <p>soapserver.php 此文件做服务端软件,让客户端请求<br /> </p>    <pre class="brush:php; toolbar: true; auto-links: false;"><?php require (‘./addclass.php’); $soap = new SoapServer(“./test.wsdl”); $soap->setClass(“addclass”); $soap->handle(); ?></pre>    <p></p>    <p>soapclient.php 客户端请求示例<br /> </p>    <pre class="brush:php; toolbar: true; auto-links: false;"><?php $client = new SoapClient(“http://www.com/api/test.wsdl”); echo $client->add(7,8); ?></pre>    <p></p>    <p>还有一个test.wsdl文件,这个可以用zend studio直接生成,</p>    <p>说明:</p>    <p>《1》建立了类文件后就要进行生成 swdl文件了,呵呵!当然不能手写了,用zend工具生成</p>    <p>流程:</p>    <p>Tools ==> WSDL Generator ==> Configration name : test; WSDL file name: test ==>NEXT ==> 点击 +   ==> 选择上面的创建的class.php ==> 这时候会看到一个 classes   : URL Location的映射,保留 test类前面的勾,并将其url 设置为 SOAP Server的url:<a href="/misc/goto?guid=4959499533545011998" target="_blank">http://localhost:8080/server.php</a> ==>点击Finish,ZDE就会创建一个非常漂亮的WSDL了   工作基本上完成了.</p>    <p>这个swdl生成非常重要,必须按步骤来!</p>    <p>《2》类中的每个变量之前不需要加上类似的下面这种格式,这样生成swdl文件时类型才不会出错。</p>    <p>/*<br /> @param string $msg<br /> */</p>    <p>当然生成swdl文件后,要把它拷在documentroot 下。</p>    <p>WEB SERVICE就这么简单.</p>