PHP SoapClient调用响应缺少部分答案

时间:2009-12-02 05:21:14

标签: php parsing soap stdclass

我在解析SoapClient调用的响应时遇到问题。对于某些类型的答案,它返回的是空stdClass对象的数组,而不是初始化的stdClass对象。

服务器是在tomcat6上使用axis2部署的java webservice。有问题的服务调用的Java签名是public Course getCourseDetails(Long courseId)课程是标准的POJO定义为:

public class Course {
    private Long id;
    private List<Hole> holes;
    private String name;
    private String tees;

    //etc...
}

洞是标准的POJO,只有主要成员。

使用PHP调用时,孔成员是一个长度正确的数组,但每个孔都是空的。

$args = array();
$args["courseId"] = $courseId;
$response = $client->getCourseDetails($args);
$course = $response->return;
//course has all of its primitive members set correctly: good
$holes = $course->holes;
//holes is an array with count = 18: good
$hole = $holes[0];
//hole is an empty stdClass: bad

使用$soapClient->__getLastResponse()打印出返回的XML,看起来是正确的表示形式:

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getCourseDetailsResponse xmlns:ns="http://webservice.golfstats">
<ns:return xmlns:ax21="http://datastructures.server.golfstats/xsd" xmlns:ax22="http://util.java/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ax24="http://uuid.eaio.com/xsd" xsi:type="ax21:Course">
<ax21:courseLocation>Faketown, VA</ax21:courseLocation>
<ax21:courseName>Fake Links</ax21:courseName>
<ax21:dateAdded>2003-01-02</ax21:dateAdded>
<ax21:holes><ax21:id>1</ax21:id><ax21:number>1</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>2</ax21:id><ax21:number>2</ax21:number><ax21:par>3</ax21:par><ax21:yardage>150</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>3</ax21:id><ax21:number>3</ax21:number><ax21:par>5</ax21:par><ax21:yardage>502</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>4</ax21:id><ax21:number>4</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>5</ax21:id><ax21:number>5</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>6</ax21:id><ax21:number>6</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>7</ax21:id><ax21:number>7</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>8</ax21:id><ax21:number>8</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>9</ax21:id><ax21:number>9</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>10</ax21:id><ax21:number>10</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>11</ax21:id><ax21:number>11</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>12</ax21:id><ax21:number>12</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>13</ax21:id><ax21:number>13</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>14</ax21:id><ax21:number>14</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>15</ax21:id><ax21:number>15</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>16</ax21:id><ax21:number>16</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>17</ax21:id><ax21:number>17</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:holes><ax21:id>18</ax21:id><ax21:number>18</ax21:number><ax21:par>4</ax21:par><ax21:yardage>345</ax21:yardage></ax21:holes>
<ax21:id>1</ax21:id>
<ax21:rating>68.5</ax21:rating>
<ax21:slope>113</ax21:slope>
<ax21:tees>Blue</ax21:tees>
</ns:return>
</ns:getCourseDetailsResponse>
</soapenv:Body>
</soapenv:Envelope>

为什么每个洞都是空的stdClass? SoapClient将解析响应的级别数是否存在已知限制?

6 个答案:

答案 0 :(得分:4)

我有类似的问题。我经历了你经历的每一次迭代。在一个侥幸我通过更改PHP.INI文件或ini_set('soap.wsdl_cache', WSDL_CACHE_NONE);禁用缓存“soap.wsdl_cache”,并在我的下一个请求中填充所有丢失的数据。这很容易发生,因为“soap.wsdl_cache_ttl”默认设置为“86400”,即60天。

我发现肥皂服务器有代码更改。创建一个新的wsdl。客户端缓存的wsdl在那时是陈旧的。您会认为,至少每种请求都会出现某种类型的校验和散列,以验证wsdl是否已更改,但事实并非如此。

要解决此问题并仍然使用缓存,我创建了一个可以在本地使用的wsdl文件。

    $cache = Services_Utilities::getCacheResource();
    if (!$cache->test(self::CACHE_KEY)) {
        $data = file_get_contents($wsdl);
        $cache->save($data, self::CACHE_KEY);
        file_put_contents($newWsdl, $data);
        if (file_exists($newWsdl)) {
            $wsdl = $newWsdl;
        }
    } else {
        if (file_exists($newWsdl)) {
            $wsdl = $newWsdl;
        }
    }

    // Remove $newWsdl when necessary
    // unset($newWsdl);

希望这可以帮助您或其他任何碰巧遇到并遇到类似问题的人。

答案 1 :(得分:1)

你是通过调试或打印出PHP对象的内容(print_r,var_dump)来解决这个问题吗?

您是否尝试过打印出实际的SOAP响应字符串(而不是PHP对象)?您可以通过使用调试选项集创建SoapClient来执行此操作:

$soapClient = new SoapClient( "http://your.soap.server.com/services/yourWsdl.wsdl", array("trace" => 1));

然后,当您使用客户端进行SOAP调用时,您可以查看请求和响应字符串。

$response = $soapClient->getCourseDetails($params);
$requestAsString = $soapClient->__getLastRequest();
$responseAsString = $soapClient->__getLastResponse();

这可以帮助您弄清楚SoapClient在将响应转换为PHP对象时正在做什么。 More info on __getLastResponse()

答案 2 :(得分:0)

这似乎是PHP中的一个错误。 http://bugs.php.net/bug.php?id=49070

不幸的是,错误跟踪器不会让我对它发表评论。

答案 3 :(得分:0)

这里我们差不多一年半了......

在我最近的半相似经历中,这不是一个php错误。这是一个与您的Web服务编写方式以及PHP如何读取输出相关的问题。我遇到了类似的问题(甚至到getLastResponse返回正确的XML)并且发现它不是那么多PHP或我的SOAP函数有问题但是“破坏”函数的结果不是明确的定义了游标。

错误游标定义示例:

PROCEDURE GetBlahByBlahID(IN IN_BLAH_ID VARCHAR, IN IN_BLAHPKG VARCHAR,                                     
OUT result CURSOR
) BEGIN ...

良好的游标定义示例:

PROCEDURE GetBlahByBlahID(IN IN_BLAH_ID VARCHAR, IN IN_BLAHPKG VARCHAR,                                     
OUT result CURSOR (  BLAH VARCHAR(250),
                     BLAH2 VARCHAR(250),
                     BLAH_DATE DATE,
                     BLAH3 VARCHAR(250))) BEGIN ...

显然,Java可以很好地处理“坏”/非显式输出,但PHP返回一个空对象数组。

不确定这是否对您有所帮助,但将Web服务功能输出定义为解决问题的“好”方式。

答案 4 :(得分:0)

要解决此问题,您可以获取soap响应的xml字符串并将其转换为对象。

$soapClient = new \SoapClient($wsdl, ['trace' => 1]); // trace is essential to get the lastResponse string
$soapClient->__call($function_name, $arguments);
$soapResponse = $soapClient->__getLastResponse();
$filteredSoapResponse = str_ireplace("soap:", "", $soapResponse); // remove the "soap:" namespace
$responseObject = simplexml_load_string($filteredSoapResponse);

所有数据都应该在$ responseObject中,即使您以前看不到的数据也是如此。

答案 5 :(得分:0)

对我来说,问题在于模式定义中缺少字段。

<xsd:import schemaLocation="https://*****.svc?xsd=xsd2" namespace="http://FunctionName"/>

数据已正确传输,但PHP仅显示了远程模式中列出的字段。

所以我不得不重新创建服务架构。

相关问题