PHP SOAPCall 空结果

PHP SOAPCall null result

我已经部署了带有 AXIS 的 SOAP WS。我在 Ubuntu 14.04 上使用 SOAPClient 库和 PHP 5.5.9 来调用公开的操作,但是当我创建“__SoapCall”时,它 return 什么都没有。我也尝试过 "NuSOAP" 库,但我得到了相同的结果。 但是当我调用“__getLastResponse”时,它 return 是好的响应(尽管是重复的),如您所见:

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <idXMLReturn xmlns="http://aemetproyecto">PD94bWwgdm...</idXMLReturn>
    </soapenv:Body>
</soapenv:Envelope>

<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <idXMLReturn xmlns="http://aemetproyecto">PD94bWwgdm...</idXMLReturn>
    </soapenv:Body>
</soapenv:Envelope>

Here is the WSDL

我的PHP代码:

<?php

    function generarTabla($id, $formato){
        try{

            // Create the SoapClient instance 
            $url = "http://localhost:8080/axis/services/AemetProyect?wsdl";

            $client = new SoapClient($url, array("trace" => 1, "exception" => 1));

            // Creo XML
            $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
            $xml .= "<!DOCTYPE id [\n";
            $xml .= "<!ELEMENT id (#PCDATA)>\n";
            $xml .= "]>\n";
            $xml .= "<id>".$id."</id>";

            // Codifico XML en Base64
            $Base64xml = base64_encode($xml);

            // Llamada SOAP a DescargarInfoTiempo
            $resultado = $client -> __soapCall("DescargarInfoTiempo",
                                                array($Base64xml));

            //$resultado = $client -> __getLastResponse();
            //echo $resultado;
            //$resultado = $client -> __getLastRequest();
            //echo $resultado;

            echo $resultado;
        } catch (SoapFault $ex){
            $error = "SOAP Fault: (faultcode: {$ex->faultcode}\n"
                                ."faultstring: {$ex->faultstring})";
            echo $error;
        } catch (Exception $e){
            $error = "Exception: {$e->faultstring}";
            echo $error;
        }
    }

?>

我注意到 return 元素的名称 ("idXMLReturn") 与 WSDL ("DescargarInfoTiempoReturn") 中描述的名称不同。 这可能是问题所在吗?

更新

我试过:

$argumens['idXML'] = $Base64xml;
$resultado = $client -> __soapCall("DescargarInfoTiempo",
                                                    array($arguments));

$arguments['idXML'] = $Base64xml;
$resultado = $client ->DescargarInfoTiempo($arguments);

但是当我打电话的时候我得到 "Notice: Array to string conversion"。

你的错误有点详细。

对于使用 SoapClient 的调用,您必须以参数数组的形式发送参数,而不是发送一个 xml(为什么您需要进行编码 base64?是您的 WebService 的一些规则吗?) .请参阅 PHP docs 以获得正确的方法

arguments An array of the arguments to pass to the function. This can be either an ordered or an associative array. Note that most SOAP servers require parameter names to be provided, in which case this must be an associative array.

所以,你的数组参数*必须是这样的:

$arguments['id'] = $id// if you need base 64, use base64_encode($id) or something
$resultado = $client -> __soapCall("DescargarInfoTiempo",array($arguments));

做同样事情的其他选项,是直接调用函数:

$arguments['id'] = $id// if you need base 64, use base64_encode($id) or something
    $resultado = $client ->DescargarInfoTiempo($arguments);// note that we don't need array($arguments), just $arguments

查看文档页面评论中的其他示例。

最后,如果您仍想创建要发送的 XML,我建议您使用 file_get_contents 或 CURL 等其他方法来完成,并且不要忘记创建 [=62] =] 带有肥皂信封,Soap Protocol

需要

*一些旧的 WebServices 需要一个名称为 "parameters"

的数组

更新

看,您正在尝试将 $arguments 发送到一个仅包含一个元素的数组中:$Base64xml 变量中的 XML。我认为问题仍然存在。

根据 PHP manual您不能在 SoapCall 中发送 XML。 var 必须是一个 与你的 vars 关联的数组,所以尝试做这样的事情:

$arguments['id'] = $id// this $id var is your function argument(int, string or somenthing else), forget the created XML
    $resultado = $client -> __soapCall("DescargarInfoTiempo",array($arguments));

关于你需要的base64,我以前从来不需要,但是看到PHP manual

这一页的marcovtwout评论

If your WSDL file containts a parameter with a base64Binary type, you should not use base64_encode() when passing along your soap vars. When doing the request, the SOAP library automatically base64 encodes your data, so otherwise you'll be encoding it twice.

所以,我相信您不需要对变量进行编码。

简而言之,忘记 XML 并只发送您的变量。 PHP SoapClient 使用更正的编码和所有这些东西创建 Soap 包络。

如果您在执行此操作时仍然遇到问题,请尝试用一些 SoapVars 将您的 var 括起来。也许您的 WSDL 配置需要这种处理方式。

希望对您有所帮助

正如我上面所说,"I've noticed that the name of return element ("idXMLReturn") 与 WSDL ("DescargarInfoTiempoReturn")" 中描述的名称不同 所以,这就是问题所在:AXIS 不生成适合 WSDL 模式的信封。似乎 java:RPC 提供者并不担心操作的 return 名称。

现在,我已经通过将参数从“idXML”重命名为“DescargarInfoTiempo”解决了这个问题,因此 SOAP 响应将是“DescargarInfoTiempoReturn”并且它适合使用 WSDL 架构和 PHP 将正确映射响应。

解决方案是指定文档文字样式,如中所述 Creating a SOAP call using PHP with an XML body