首页 > 解决方案 > 具有多个层和属性的 SOAP 客户端

问题描述

我花了几个小时试图弄清楚这一点,但我从 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 属性,要么它返回一个肥皂错误,说设备信息或凭据错误,我知道它们都是输入的正确并正确传递到包装函数变量中,因此它们被正确传递给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));

标签: phpsoapsoap-client

解决方案


以下是我通常登录和发送 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);

推荐阅读