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}]" 

调用成功

 

转载于:https://www.cnblogs.com/mbailing/p/3998821.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值