具有多层和属性的SOAP客户端

时间:2019-06-03 18:49:19

标签: php soap soap-client

我花了数小时试图弄清楚这一点,但从SO到PHP.net的建议我都没有尝试过。我试图让SOAP调用正常工作,因为我有多个嵌套的XML级别,并且在顶层和子级都有属性,但似乎没有任何效果。我的代码哪里出问题了?

我已经尝试过SO和PHP.net中的所有内容,但是似乎没有一个答案足够深入或涉及多个XML级别,它们似乎都假设您只深入了一个级别。

除了更多内容,我还尝试了以下两种方法:

    $params = array("Request"=>array("_"=>array("Credentials"=>array("UserNumberCredentials"=>array("UserNumber"=>$userNumber,"Password"=>$password)),"DeviceInformation"=>array("_"=>"","DeviceType"=>$this->deviceType,"DeviceNumber"=>$this->deviceNumber)),"MessageId"=>$this->messageId));
    $params = array("Request"=>array("_"=>array("Credentials"=>array("_"=>array("UserNumberCredentials"=>array("_"=>array("UserNumber"=>$userNumber,"Password"=>$password)))),"DeviceInformation"=>array("_"=>"","DeviceType"=>$this->deviceType,"DeviceNumber"=>$this->deviceNumber)),"MessageId"=>$this->messageId));

预期的XML是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
   <soapenv:Header/>
   <soapenv:Body>
      <user:logon>
         <!--Optional:-->
         <Request MessageId="messageId">
            <!--Optional:-->
            <Credentials>
               <!--Optional:-->
               <UserNumberCredentials>
                  <!--Optional:-->
                  <UserNumber>value</UserNumber>
                  <!--Optional:-->
                  <Password>value</Password>
               </UserNumberCredentials>
            </Credentials>
            <!--Optional:-->
            <DeviceInformation DeviceType="deviceType" DeviceNumber="number" />
         </Request>
      </user:logon>
   </soapenv:Body>
</soapenv:Envelope>

我正在像这样将参数传递到SOAP调用中:

    $results = $this->client->logon($params);

我已经尝试了多种方法,要么返回一个验证错误,或者说它在请求标签上缺少MessageId属性,要么返回一个错误消息,指出设备信息或凭据错误,我知道它们是re都正确键入并正确传递到包装函数变量中,因此它们已通过soap调用正确传递。但是因为它返回了一个肥皂错误,所以我无法知道它正在传递的实际形成的XML。

更新: 以下参数是正确的,但是我认为两个DeviceInformation属性均未发送。我认为它只发送一个,因此服务器拒绝了呼叫。 DeviceInformation标记本身为空,但是该标记中都需要DeviceNumber和DeviceType属性,并且我认为在调用中仅发送一个或不发送任何消息。但这返回了错误,因此我无法看到XML。

$params = array("Request"=>array("_"=>array("Credentials"=>array("UserNumberCredentials"=>array("UserNumber"=>$userNumber,"Password"=>$password)),"DeviceInformation"=>array("_"=>"","DeviceType"=>$this->deviceType,"DeviceNumber"=>$this->deviceNumber)),"MessageId"=>$this->messageId));

2 个答案:

答案 0 :(得分:0)

这是我通常登录并发送SOAP请求的方式:

<?php
// The form
$params = array(
    "Request"=>array(
        "MessageId"=>$this->messageId,
        "Credentials"=>array(
            "UserNumberCredentials"=>array(
                "UserNumber"=>$userNumber,
                "Password"=>$password
            )
        ),
        "DeviceInformation"=>array(
            "DeviceType"=>$this->deviceType,
            "DeviceNumber"=>$this->deviceNumber
        )
    )
);

// API Configs
$config = array(
    "wsdl" => "https:// ... the wsdl url ... ", // WSDL URL Here
    "namespace" => "http://schemas.xmlsoap.org/soap/envelope/",
    "username" => "", // Username
    "password" => "", // Password
    "soap_options" => array(
        "exceptions" => true,
        "trace" => 1,
        /* If you need a proxy, uncomment
        "proxy_host" => "",
        "proxy_port" => 80,
        */
        "cache_wsdl" => WSDL_CACHE_NONE
    ),
    // For SSL..
    "stream_context" => stream_context_create(array(
        "ssl" => array(
            // set some SSL/TLS specific options
            "verify_peer" => false,
            "verify_peer_name" => false,
            "allow_self_signed" => true
        )
    ))
);

// Initialize soap client object
$client = new SoapClient($config, $config["soap_options"]);

// Credentials
$credentials = array(
    "userName" => $config["username"], 
    "password" => $config["password"]
);
$authvalues = new SoapVar($credentials, SOAP_ENC_OBJECT);

// Headers
$header = new SoapHeader($config["namespace"], "AuthenticationInfo", $authvalues, false);
$client->__setSoapHeaders(array($header));

// Response
$resp = array(
    "status" => "success"
);

// Request
$req = array();
try {
    // SOAP Request 
    $req = $client->logon($params); // Here is the WS Method you want to call.
    $resp["result"] = $req;
} catch(Exception $e) {
    $resp["status"] = "error";
    $resp["details"] = $e;
    $resp["result"] = $req;
    $resp["soap_form"] = $form;
}

echo json_encode($resp);

答案 1 :(得分:0)

显然,以下是应该格式化数组以获取参数中属性的方式。我从使用相同服务的某人那里获得了示例代码,这与他们对其进行格式化的方式类似,并且可以正常工作。这与我用Google搜索的内容完全不同,但也许它将对以后的人有所帮助。作为记录,我的SoapClient处于WSDL模式。

            $params = array(
                "Request"=>array(
                    "MessageId"=>$this->messageId,
                    "Credentials"=>array(
                        "UserNumberCredentials"=>array(
                            "UserNumber"=>$userNumber,
                            "Password"=>$password
                        )
                    ),
                    "DeviceInformation"=>array(
                        "DeviceType"=>$this->deviceType,
                        "DeviceNumber"=>$this->deviceNumber
                    )
                )
            );