PHP Webservice的发布与调用

1.  环境配置

配置php.ini,把php_soap.dll前面的分号去掉,

 

不然会报错

class soapserver not found

 

 

重启apache后通过phpinfo()查看

这样是表示环境已经支持soap的webservice了,后面的事情就是写代码了。

 

 

2.  webservice的发布

发布出来的*.wsdl文件,其实是一个xml格式的文件,生成这个文件可以通过第3方软件,如ZendStudio 就可以生成。

 

示例,我用以下代码生成:

  1. <?php  
  2.   
  3. class CTest  
  4.   
  5. {  
  6.   
  7.        public function __construct()  
  8.   
  9.        {  
  10.   
  11.                 
  12.   
  13.        }  
  14.   
  15.          
  16.   
  17.        /** 
  18.  
  19.         * 
  20.  
  21.         * @param string $oParams 
  22.  
  23.         * @return string 
  24.  
  25.         */  
  26.   
  27.        public function Add($oParams)  
  28.   
  29.        {  
  30.   
  31.               $sParams = $oParams->oParams[l1] ;  
  32.   
  33.               $oParams = json_decode($sParams);  
  34.   
  35.               $a = $oParams->a;  
  36.   
  37.               $b = $oParams->b;  
  38.   
  39.               $c = $a+$b;  
  40.   
  41.               return array('AddResult'=>$c);  
  42.   
  43.        }  
  44.   
  45. }  
  46.   
  47. ?>  


 

生成的xml文件是:

  1. <?xml version="1.0" encoding="UTF-8" ?>   
  2.   
  3.  <wsdl:definitions targetNamespace="http://tempuri.org/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">  
  4.   
  5.  <wsdl:types>  
  6.   
  7.  <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">  
  8.   
  9.  <s:element name="AddRequest">  
  10.   
  11.  <s:complexType>  
  12.   
  13.  <s:sequence>  
  14.   
  15.   <s:element minOccurs="0" maxOccurs="1" name="oParams" type="s:string" />   
  16.   
  17.   </s:sequence>  
  18.   
  19.   </s:complexType>  
  20.   
  21.   </s:element>  
  22.   
  23.  <s:element name="AddResponse">  
  24.   
  25.  <s:complexType>  
  26.   
  27.  <s:sequence>  
  28.   
  29.   <s:element minOccurs="0" maxOccurs="1" name="AddResult" type="s:string" />   
  30.   
  31.   </s:sequence>  
  32.   
  33.   </s:complexType>  
  34.   
  35.   </s:element>  
  36.   
  37.   </s:schema>  
  38.   
  39.   </wsdl:types>  
  40.   
  41.  <wsdl:message name="AddSoapIn">  
  42.   
  43.   <wsdl:part name="parameters" element="tns:AddRequest" />   
  44.   
  45.   </wsdl:message>  
  46.   
  47.  <wsdl:message name="AddSoapOut">  
  48.   
  49.   <wsdl:part name="parameters" element="tns:AddResponse" />   
  50.   
  51.   </wsdl:message>  
  52.   
  53.  <wsdl:portType name="CTestSoap">  
  54.   
  55.  <wsdl:operation name="Add">  
  56.   
  57.   <wsdl:input message="tns:AddSoapIn" />   
  58.   
  59.   <wsdl:output message="tns:AddSoapOut" />   
  60.   
  61.   </wsdl:operation>  
  62.   
  63.   </wsdl:portType>  
  64.   
  65.  <wsdl:binding name="CTestSoap" type="tns:CTestSoap">  
  66.   
  67.   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />   
  68.   
  69.  <wsdl:operation name="Add">  
  70.   
  71.   <soap:operation soapAction="http://tempuri.org/Add" style="document" />   
  72.   
  73.  <wsdl:input>  
  74.   
  75.   <soap:body use="literal" />   
  76.   
  77.   </wsdl:input>  
  78.   
  79.  <wsdl:output>  
  80.   
  81.   <soap:body use="literal" />   
  82.   
  83.   </wsdl:output>  
  84.   
  85.   </wsdl:operation>  
  86.   
  87.   </wsdl:binding>  
  88.   
  89.  <wsdl:binding name="CTestSoap12" type="tns:CTestSoap">  
  90.   
  91.   <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />   
  92.   
  93.  <wsdl:operation name="Add">  
  94.   
  95.   <soap12:operation soapAction="http://tempuri.org/Add" style="document" />   
  96.   
  97.  <wsdl:input>  
  98.   
  99.   <soap12:body use="literal" />   
  100.   
  101.   </wsdl:input>  
  102.   
  103.  <wsdl:output>  
  104.   
  105.   <soap12:body use="literal" />   
  106.   
  107.   </wsdl:output>  
  108.   
  109.   </wsdl:operation>  
  110.   
  111.   </wsdl:binding>  
  112.   
  113.  <wsdl:service name="CTest">  
  114.   
  115.  <wsdl:port name="CTestSoap" binding="tns:CTestSoap">  
  116.   
  117.   <soap:address location="http://192.168.6.44:80/webservice/server/server.php?wsdl[l2] " />   
  118.   
  119.   </wsdl:port>  
  120.   
  121.  <wsdl:port name="CTestSoap12" binding="tns:CTestSoap12">  
  122.   
  123.   <soap12:address location="http://192.168.6.44:80/ webservice/server/server.php?wsdl[l3] " />   
  124.   
  125.   </wsdl:port>  
  126.   
  127.   </wsdl:service>  
  128.   
  129.   </wsdl:definitions>  


 

 

生成xml文件后,就要通过一个服务发布出去,如:

  1. <?php  
  2.   
  3. error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);   
  4.   
  5. include_once('../class/test.class.php');[l4]   
  6.   
  7. $server = new SoapServer('wscache/CTest.1.0.wsdl');  
  8.   
  9. $server->setClass('CTest');  
  10.   
  11. $server->handle();  
  12.   
  13. ?>  


 

然后可以向其他人或者系统提供出发布的地址,如:

http://192.168.6.44/webservice/server/server.php?wsdl

 

3.  webservice的调用

通过上面提供的地址,写一个调用页面

  1. <?php  
  2.   
  3. error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);   
  4.   
  5. header("Content-Type: text/html;charset=utf-8");  
  6.   
  7. $client = new SoapClient('http://192.168.6.44/webservice/server/server.php?wsdl');  
  8.   
  9. $str = '{"a":1,"b":20}';  
  10.   
  11. //调用方法一  
  12.   
  13. $r = $client->Add(array('oParams'=>$str)); //数组  
  14.   
  15. //调用方法二  
  16.   
  17. //$pParams->oParams = $str;  
  18.   
  19. //$r = $client->__call('Add',array($pParams));//这个得是对象  
  20.   
  21. var_dump($r);  
  22.   
  23. ?> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值