首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >PHP Webservice的发布与调用

PHP Webservice的发布与调用

作者头像
Java架构师必看
发布2021-03-22 10:37:43
发布2021-03-22 10:37:43
1.4K00
代码可运行
举报
文章被收录于专栏:Java架构师必看Java架构师必看
运行总次数:0
代码可运行

PHP Webservice的发布与调用

1.  环境配置

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

不然会报错

class soapserver not found

重启apache后通过phpinfo()查看

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

2.  webservice的发布

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

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

代码语言:javascript
代码运行次数:0
运行
复制
<?php

class CTest

{

       public function __construct()

       {

              

       }

       

       /**

        *

        * @param string $oParams

        * @return string

        */

       public function Add($oParams)

       {

              $sParams = $oParams->oParams[l1] ;

              $oParams = json_decode($sParams);

              $a = $oParams->a;

              $b = $oParams->b;

              $c = $a+$b;

              return array('AddResult'=>$c);

       }

}

?>

生成的xml文件是:

代码语言:javascript
代码运行次数:0
运行
复制
<?xml version="1.0" encoding="UTF-8" ?> 

 <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/">

 <wsdl:types>

 <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">

 <s:element name="AddRequest">

 <s:complexType>

 <s:sequence>

  <s:element minOccurs="0" maxOccurs="1" name="oParams" type="s:string" /> 

  </s:sequence>

  </s:complexType>

  </s:element>

 <s:element name="AddResponse">

 <s:complexType>

 <s:sequence>

  <s:element minOccurs="0" maxOccurs="1" name="AddResult" type="s:string" /> 

  </s:sequence>

  </s:complexType>

  </s:element>

  </s:schema>

  </wsdl:types>

 <wsdl:message name="AddSoapIn">

  <wsdl:part name="parameters" element="tns:AddRequest" /> 

  </wsdl:message>

 <wsdl:message name="AddSoapOut">

  <wsdl:part name="parameters" element="tns:AddResponse" /> 

  </wsdl:message>

 <wsdl:portType name="CTestSoap">

 <wsdl:operation name="Add">

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

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

  </wsdl:operation>

  </wsdl:portType>

 <wsdl:binding name="CTestSoap" type="tns:CTestSoap">

  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 

 <wsdl:operation name="Add">

  <soap:operation soapAction="http://tempuri.org/Add" style="document" /> 

 <wsdl:input>

  <soap:body use="literal" /> 

  </wsdl:input>

 <wsdl:output>

  <soap:body use="literal" /> 

  </wsdl:output>

  </wsdl:operation>

  </wsdl:binding>

 <wsdl:binding name="CTestSoap12" type="tns:CTestSoap">

  <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /> 

 <wsdl:operation name="Add">

  <soap12:operation soapAction="http://tempuri.org/Add" style="document" /> 

 <wsdl:input>

  <soap12:body use="literal" /> 

  </wsdl:input>

 <wsdl:output>

  <soap12:body use="literal" /> 

  </wsdl:output>

  </wsdl:operation>

  </wsdl:binding>

 <wsdl:service name="CTest">

 <wsdl:port name="CTestSoap" binding="tns:CTestSoap">

  <soap:address location="http://192.168.6.44:80/webservice/server/server.php?wsdl[l2] " /> 

  </wsdl:port>

 <wsdl:port name="CTestSoap12" binding="tns:CTestSoap12">

  <soap12:address location="http://192.168.6.44:80/ webservice/server/server.php?wsdl[l3] " /> 

  </wsdl:port>

  </wsdl:service>

  </wsdl:definitions>

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

代码语言:javascript
代码运行次数:0
运行
复制
<?php

error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); 

include_once('../class/test.class.php');[l4] 

$server = new SoapServer('wscache/CTest.1.0.wsdl');

$server->setClass('CTest');

$server->handle();

?>

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

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

3.  webservice的调用

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

代码语言:javascript
代码运行次数:0
运行
复制
<?php

error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE); 

header("Content-Type: text/html;charset=utf-8");

$client = new SoapClient('http://192.168.6.44/webservice/server/server.php?wsdl');

$str = '{"a":1,"b":20}';

//调用方法一

$r = $client->Add(array('oParams'=>$str)); //数组

//调用方法二

//$pParams->oParams = $str;

//$r = $client->__call('Add',array($pParams));//这个得是对象

var_dump($r);

?>

本文由来源 21aspnet,由 javajgs_com 整理编辑,其版权均为 21aspnet 所有,文章内容系作者个人观点,不代表 Java架构师必看 对观点赞同或支持。如需转载,请注明文章来源。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • class soapserver not found
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档