设为首页 收藏本站
查看: 1290|回复: 0

[经验分享] ZendStudio php WebService制作一:简单WebService Demo

[复制链接]

尚未签到

发表于 2015-8-23 14:44:06 | 显示全部楼层 |阅读模式
在网上找了很多制作PHP的Web Service的例子,但是都不太全面。以下是我的php制作过程。
1. 首先制作提供WebService的php页面。

DSC0000.gif //myservice.php
<?php
class service
{
  public function HelloWorld()
  {
      return  "Hello";
  }
  public  function Add($a,$b)
  {
      return $a+$b;
  }
}
//TestSoap.wsd需要在后面采用Zend Studio生成。
$server=new SoapServer('TestSoap.wsdl',array('soap_version' => SOAP_1_2));
$server->setClass("service");
$server->handle();
?>
2.生成WSDL文件。采用Zend Studio生成。
生成过程:选择菜单File-->Export-->PHP->WSDL FILE
也就是出现下面的图
DSC0001.jpg
选择 next ,出现如下WSDL选择画面
DSC0002.jpg
File Name:选择需要生成的WSDL文件存储文件名和路径
Exported file 选择学要前面写的myservice.php文件,可以添加多个。
Classes:选择需要制作WebService的文件,注意:URL:一定要写上myservice.php的 URL.如果提供服务的是其他php页面,那就换成其他的php服务页面。
Global Settings:如下图所示:
DSC0003.jpg
选择Finish,生成WSDL文件,下面是Zend Studio里的效果图
DSC0004.jpg
3.客户端调用该Web Service

//Client.php
<?php
$a=new SoapClient("TestSoap.wsdl");
echo $a->HelloWorld();
echo("<br />");
echo $a->Add(1,2);
?>
到此为止,一个简单的Php WebService制作完成。
扩展:如何用WebService实现数据库的GRUD操作,后续发布。
补充:
TestSoap.wsdl文件到底是什么

<?xml version='1.0' encoding='UTF-8'?>
<!-- WSDL file generated by Zend Studio. -->
<definitions name="TestSoap" targetNamespace="urn:TestSoap" xmlns:typens="urn:TestSoap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
    <message name="Add">
        <part name="a"/>
        <part name="b"/>
    </message>
    <message name="AddResponse">
        <part name="AddReturn"/>
    </message>
    <message name="HelloWorld"/>
    <message name="HelloWorldResponse">
        <part name="HelloWorldReturn"/>
    </message>
    <message name="Sub">
        <part name="a"/>
    </message>
    <message name="SubResponse">
        <part name="SubReturn"/>
    </message>
    <portType name="servicePortType">
        <operation name="Add">
            <input message="typens:Add"/>
            <output message="typens:AddResponse"/>
        </operation>
        <operation name="HelloWorld">
            <input message="typens:HelloWorld"/>
            <output message="typens:HelloWorldResponse"/>
        </operation>
        <operation name="Sub">
            <input message="typens:Sub"/>
            <output message="typens:SubResponse"/>
        </operation>
    </portType>
    <binding name="serviceBinding" type="typens:servicePortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="Add">
            <soap:operation soapAction="urn:serviceAction"/>
            <input>
                <soap:body namespace="urn:TestSoap" use="literal"/>
            </input>
            <output>
                <soap:body namespace="urn:TestSoap" use="literal"/>
            </output>
        </operation>
        <operation name="HelloWorld">
            <soap:operation soapAction="urn:serviceAction"/>
            <input>
                <soap:body namespace="urn:TestSoap" use="literal"/>
            </input>
            <output>
                <soap:body namespace="urn:TestSoap" use="literal"/>
            </output>
        </operation>
        <operation name="Sub">
            <soap:operation soapAction="urn:serviceAction"/>
            <input>
                <soap:body namespace="urn:TestSoap" use="literal"/>
            </input>
            <output>
                <soap:body namespace="urn:TestSoap" use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="TestSoapService">
        <port name="servicePort" binding="typens:serviceBinding">
            <soap:address location="http://localhost/test/soap/myservice.php"/>
        </port>
    </service>
</definitions>
但是我发觉他和.net 产生的WSDL不一样。而且通过这种方式有一个弊端,如果我的Service里添加了一个新的方法,需要在重新生成WSDL文件,
而且还经常出错误。但是如果采用Zend WSDL编辑器产生的WSDL感觉更标准写,而且他相对于目前这样的方法不需要删除WSDL重新建,只需要编辑目前已有的。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-103044-1-1.html 上篇帖子: PHP过滤器 下篇帖子: PHP 关于文件上传下载 断点续传问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表