php 使用zendstudio 生成webservice文件 wsdl

首先新建一个项目

在项目中新建下面这些文件

php类文件 test.php

复制代码
<?php
class test {
    public function __construct()
    {
    }
    public function add($name,$age)
    {
        $result = array('REV'=>false);
        $result['REV'] = true;
        $result['DATA'] = 1;
        $result = json_encode($result);
        return $result;
    }
    public function del($id)
    {
        $result = false;
        return $result;
    }
    public function getlist($type)
    {
        $result = array(
            array('name'=>'张三','age'=>18),
            array('name'=>'李四','age'=>20),
            array('name'=>'jms','age'=>10),
            array('name'=>'jk陈','age'=>8),
        );
        $result = json_encode($result);
        return $result;
    }
}

?>
复制代码

使用zendstudio生成wsdl文件

 

 

生成的文件格式如下

复制代码
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost/t/ws" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="test_server" targetNamespace="http://localhost/t/ws">
  <wsdl:types>
    <xsd:schema targetNamespace="http://localhost/t/ws">
      <xsd:element name="getlist">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="in" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
      <xsd:element name="getlistResponse">
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name="out" type="xsd:string"/>
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="getlistRequest">
    <wsdl:part name="type" type="xsd:int"/>
  </wsdl:message>
  <wsdl:message name="getlistResponse">
    <wsdl:part name="resultRespose" type="xsd:string"/>
  </wsdl:message>
  <wsdl:portType name="test_server">
    <wsdl:operation name="getlist">
      <wsdl:input message="tns:getlistRequest"/>
      <wsdl:output message="tns:getlistResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="test_serverSOAP" type="tns:test_server">
      <soap:binding style="document"
          transport="http://schemas.xmlsoap.org/soap/http" />
      <wsdl:operation name="getlist">
          <soap:operation
              soapAction="http://localhost/t/ws/NewOperation" />
          <wsdl:input>
              <soap:body use="literal" />
          </wsdl:input>
          <wsdl:output>
              <soap:body use="literal" />
          </wsdl:output>
      </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="test_server">
    <wsdl:port binding="tns:test_serverSOAP" name="test_serverSOAP">
      <soap:address location="http://localhost/t/ws/server.php"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
复制代码

 

调用server的文件 server.php

<?php
ini_set("soap.wsdl_cache_enabled", "0");  //测试时打开防止soap缓存
include("test.php");
$Server=new SoapServer('test_server.wsdl');   //SoapServer
$Server->setClass("test");
$Server->handle();
?>

 

 

测试调用webserver wsdl的文件

复制代码
<?php
ini_set("soap.wsdl_cache_enabled", "0");
//$url = 'http://localhost/t/ws/test_server.wsdl';
$url = 'http://localhost/t/ws/server.php?wsdl';    //两种url都可以
$client = new SoapClient($url);
$params = array('type'=>1);
$res = $client->__soapCall('getlist',array('parameters'=>$params));
var_dump($res);
?>
复制代码

返回数据:

string(119) "[{"name":"\u5f20\u4e09","age":18},{"name":"\u674e\u56db","age":20},{"name":"jms","age":10},{"name":"jk\u9648","age":8}]" 

调用成功

 

posted @   jerry-Tom  阅读(5022)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?
· 《HelloGitHub》第 108 期
点击右上角即可分享
微信分享提示