草庐IT

php - SOAP 服务通过 WSDL 返回空数组而不是数据库中的数据

coder 2023-10-13 原文

我正在尝试编写自己的 SOAP 服务器并通过 SoapClient(wsdl 模式)调用方法。我在 php 中创建了方法,添加了一个自动生成的 wsdl 文件。我通过 SoapClient 发送请求,服务器应该使用 Mysql 并返回结果,但我总是得到空响应。我检查了 MySQL 中的日志,它们显示了应该返回数据的正确请求。

public function getCarMakes()
{
    $carMakesArr = array();
    $sql = "SELECT * FROM cars order by make";
    try
    {
        foreach($this->conn->query($sql) as $row)
        {
            $carMakesArr[] = array( $row[0], $row[1], $row[2]);
        }
    }
    catch(Exception $e)
    {
        echo $e->getMessage();
    }

        return $carMakesArr;
}

我的 WSDL 文件:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!-- Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is Metro/2.3.1-b419 (branches/2.3.1.x-7937; 2014-08-04T08:11:03+0000) JAXWS-RI/2.2.10-b140803.1500 JAXWS-API/2.2.11 JAXB-RI/2.2.10-b140802.1033 JAXB-API/2.2.12-b140109.1041 svn-revision#unknown. -->
<definitions targetNamespace="http://wsdl.example.org/" name="ServerWS" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:tns="http://wsdl.example.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata">
  <types>
    <xsd:schema>
      <xsd:import namespace="http://wsdl.example.org/" schemaLocation="ServerWS_schema1.xsd"/>
    </xsd:schema>
  </types>
  <message name="buyCar">
    <part name="parameters" element="tns:buyCar"/>
  </message>
  <message name="buyCarResponse">
    <part name="parameters" element="tns:buyCarResponse"/>
  </message>
  <message name="getCarMakes">
    <part name="parameters" element="tns:getCarMakes"/>
  </message>
  <message name="getCarMakesResponse">
    <part name="parameters" element="tns:getCarMakesResponse"/>
  </message>
  <message name="getCarDetailsById">
    <part name="parameters" element="tns:getCarDetailsById"/>
  </message>
  <message name="getCarDetailsByIdResponse">
    <part name="parameters" element="tns:getCarDetailsByIdResponse"/>
  </message>
  <message name="searchByParams">
    <part name="parameters" element="tns:searchByParams"/>
  </message>
  <message name="searchByParamsResponse">
    <part name="parameters" element="tns:searchByParamsResponse"/>
  </message>
  <portType name="ServerWS">
    <operation name="buyCar">
      <input wsam:Action="http://wsdl.example.org/ServerWS/buyCarRequest" message="tns:buyCar"/>
      <output wsam:Action="http://wsdl.example.org/ServerWS/buyCarResponse" message="tns:buyCarResponse"/>
    </operation>
    <operation name="getCarMakes">
      <input wsam:Action="http://wsdl.example.org/ServerWS/getCarMakesRequest" message="tns:getCarMakes"/>
      <output wsam:Action="http://wsdl.example.org/ServerWS/getCarMakesResponse" message="tns:getCarMakesResponse"/>
    </operation>
    <operation name="getCarDetailsById">
      <input wsam:Action="http://wsdl.example.org/ServerWS/getCarDetailsByIdRequest" message="tns:getCarDetailsById"/>
      <output wsam:Action="http://wsdl.example.org/ServerWS/getCarDetailsByIdResponse" message="tns:getCarDetailsByIdResponse"/>
    </operation>
    <operation name="searchByParams">
      <input wsam:Action="http://wsdl.example.org/ServerWS/searchByParamsRequest" message="tns:searchByParams"/>
      <output wsam:Action="http://wsdl.example.org/ServerWS/searchByParamsResponse" message="tns:searchByParamsResponse"/>
    </operation>
  </portType>
  <binding name="ServerWSPortBinding" type="tns:ServerWS">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
    <operation name="buyCar">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="getCarMakes">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="getCarDetailsById">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <operation name="searchByParams">
      <soap:operation soapAction=""/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
  </binding>
  <service name="ServerWS">
    <port name="ServerWSPort" binding="tns:ServerWSPortBinding">
      <soap:address location="http://localhost/soap/server.php"/>
    </port>
  </service>
</definitions>

我的 XML 模式:

<xs:complexType name="getCarMakes">
    <xs:sequence>
      <xs:element name="id_array1" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="getCarMakesResponse">
    <xs:sequence>
      <xs:element name="return" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

我通过 SoapClient 的请求:

$client = new SoapClient('http://localhost/soap/ServerWS.wsdl',
                        array('cache_wsdl' => WSDL_CACHE_NONE, 
                              'trace' => 1,
                              'soap_version' => SOAP_1_2)
                             );

try
    {

        $arr = (array)$client->getCarMakes();

        echo "<b>First method:</b> <br>";

    }
    catch(SoapFault $e)
    {
        echo $e->getMessage();
    }
    echo $client->__getLastRequest();
    echo $client->__getLastResponse(); 
     var_dump($arr);

它返回一个空数组,但是数据库和数据都在那里。对数据库的请求是正确的,我最初在非 wsdl 模式下创建了服务器并且它工作得很好。

我的要求:

<!--?xml version="1.0" encoding="UTF-8"?-->
<env:envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://wsdl.example.org/">
<env:body><ns1:getcarmakes></ns1:getcarmakes>
   </env:body></env:envelope>

响应,我得到:

  <env:envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://wsdl.example.org/">
    <env:body><ns1:getcarmakesresponse></ns1:getcarmakesresponse>
    </env:body>
  </env:envelope>

请指教。

最佳答案

至少没有对复杂类型的正确定义。下面是返回可用汽车品牌列表的基本实现(作为字符串数组)。如果返回格式不同,carMakesArray 复杂类型的定义应相应调整。

将以下文件放在同一级别,并将所有文件中的 your.host 替换为可访问这些文件的实际基本 URL。

ServerWS.wsdl

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions targetNamespace="http://your.host/"
             name="ServerWS"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:tns="http://your.host/"
             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:wsam="http://www.w3.org/2007/05/addressing/metadata"
>
    <types>
        <xsd:schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://your.host/">
            <complexType name="carMakesArray">
                <complexContent>
                    <restriction base="soapenc:Array">
                        <attribute ref="soapenc:arrayType" wsdl:arrayType="xsd:string[]"/>
                    </restriction>
                </complexContent>
            </complexType>

            <xsd:complexType name="getCarMakesResponse">
                <xsd:sequence>
                    <xsd:element name="return" type="tns:carMakesArray"/>
                </xsd:sequence>
            </xsd:complexType>        
        </xsd:schema>
    </types>

    <message name="getCarMakes" />
    <message name="getCarMakesResponse">
        <part name="parameters" element="tns:getCarMakesResponse"/>
    </message>

    <portType name="ServerWS">
        <operation name="getCarMakes">
            <input wsam:Action="http://your.host/ServerWS/getCarMakesRequest" message="tns:getCarMakes"/>
            <output wsam:Action="http://your.host/ServerWS/getCarMakesResponse" message="tns:getCarMakesResponse"/>
        </operation>
    </portType>

    <binding name="ServerWSPortBinding" type="tns:ServerWS">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
        <operation name="getCarMakes">
            <soap:operation soapAction=""/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>

    <service name="ServerWS">
        <port name="ServerWSPort" binding="tns:ServerWSPortBinding">
            <soap:address location="http://your.host/soapServerTest.php"/>
        </port>
    </service>
</definitions>

soapServerTest.php

<?php
require 'CarService.php';

$soapServer = new \SoapServer('http://your.host/ServerWS.wsdl', ['cache_wsdl' => WSDL_CACHE_NONE]);
$soapServer->setClass('CarsService');
$soapServer->handle();

CarService.php

<?php
class CarsService
{
    /**
     * @return string[]
     */
    public function getCarMakes()
    {
        /** Here you may extract data from DB and return instead of hard-coded values */
        return ['BMW', 'Ford', 'Honda'];
    }
}

soapClientTest.php

<?php
$client = new SoapClient(
    'http://your.host/ServerWS.wsdl',
    array(
        'cache_wsdl' => WSDL_CACHE_NONE,
        'soap_version' => SOAP_1_2
    )
);
try {
    $makes = $client->getCarMakes();
    var_dump($makes);
} catch (SoapFault $e) {
    echo $e->getMessage();
}

关于php - SOAP 服务通过 WSDL 返回空数组而不是数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069512/

有关php - SOAP 服务通过 WSDL 返回空数组而不是数据库中的数据的更多相关文章

  1. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 多次弹出/移动 ruby​​ 数组 - 2

    我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby​​数组,我们在StackOverflow上找到一

  6. ruby - 将数组的内容转换为 int - 2

    我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]

  7. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  8. ruby - 检查数组是否在增加 - 2

    这个问题在这里已经有了答案:Checktoseeifanarrayisalreadysorted?(8个答案)关闭9年前。我只是想知道是否有办法检查数组是否在增加?这是我的解决方案,但我正在寻找更漂亮的方法:n=-1@arr.flatten.each{|e|returnfalseife

  9. ruby-on-rails - 启动 Rails 服务器时 ImageMagick 的警告 - 2

    最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru

  10. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

随机推荐