PHP 和带有复杂参数的 soap 调用

标签 php soap parameters soap-client complextype

我正在尝试传递一个相当复杂的参数字符串,我有一个 XML 示例,并且我正在尝试使用 PHP 对其进行正确编码。我收到的示例请求是这样的:

<?xml version="1.0" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAPSDK1="http://www.w3.org/2001/XMLSchema" xmlns:SOAPSDK2="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAPSDK3="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body xmlns:tns="http://172.16.53.121/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns="http://www.amtrak.com/TrainStatus/2006/01/01" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:wsdns1="http://www.amtrak.com/schema/2006/01/01" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <TrainStatusRQ xmlns="http://www.amtrak.com/schema/2006/01/01" xmlns:ota="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="1.0" EchoToken="TestToken">
        <POS>
            <ota:Source>
                <ota:RequestorID Type="WAS" ID="ToBeProvidedByAmtrak">
                    <ota:CompanyName CompanyShortName="ToBeProvidedByAmtrak"/>
                </ota:RequestorID>
            </ota:Source>
        </POS>
        <TrainStatusInfo TrainNumber="3">
            <TravelDate>
                <ota:DepartureDateTime>2006-01-07</ota:DepartureDateTime>
            </TravelDate>
            <Location LocationCode="KNG"/> 
        </TrainStatusInfo>
    </TrainStatusRQ>
</SOAP-ENV:Body>

我打算这样调用它

try
    {
       $client = new SoapClient($soapURL, $soapOptions);
       $trainStatus = $client->processTrainStatus($TrainStatusRQ);
       var_dump($trainStatus);
       //var_dump($client->__getTypes());

    }
    catch(SoapFault $e) 
    { 
        echo "<h2>Exception Error!</h2></b>"; 
        echo $e->faultstring; 
    }

它是 $TrainStatusRQ 的编码,我似乎无法弄清楚,因为它有属性和多级参数。这是我得到的最接近的结果。

$RQStruc = array(
            "POS" => array(
                "Source"=> array(
                    "RequestorID" => array(
                        'type'=>'WAS',
                        'ID'=>'0',
                        'CompanyName'=>array(
                            'CompanyShortName'=>"0"
                        )
                    )
                )
            ),
            "TrainStatusInfo" => array( 
                'TrainNumber'=>$TrainNumber, 
                'TravelDate' => array(
                        'DepartureDateTime' => array(
                                '_' => $today
                         )
                ),
                "Location" => array(
                    'LocationCode'=>$LocationCode
                )

            ) 
     );

$TrainStatusRQ = new SoapVar($RQStruc, XSD_ANYTYPE, "TrainStatusRQ","http://www.amtrak.com/schema/2006/01/01" );

最佳答案

我在处理 .NET 服务时遇到了类似的问题。

我最终得到的是将结构组装为纯字符串。

 $p = array();
    foreach ($items as $item) {

        $p[] = "
        <MyEntity class='entity'> // the attribute was required by .NET 
          <MyId>{$item->SomeID}</MyId>
          <ItemId>{$item->ItemId}</ItemId>
          <Qty>{$item->Qty}</Qty>
        </MyEntity>";

    }
    $exp = implode("\n", $p);
    $params['MyEntity'] = new \SoapVar("<MyEntity xmlns='http://schemas.microsoft.com/dynamics/2008/01/documents/MyEntity'>$exp</MyEntity>", XSD_ANYXML);

工作没有问题。

关于PHP 和带有复杂参数的 soap 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18157638/

相关文章:

php - Bud1%@@@@E%DSDB`@@@是什么?

soap - 将字节数组从 Web 服务发送到客户端

java - 如何在 Apache Axis Web 服务 (SOAP) 中添加基本身份验证?

javascript - 在 JavaScript 中获取 htaccess URL 参数

Java:方法参数中的通用枚举

php - 何时重新生成 session ID Laravel?

php - 允许并行运行php脚本

php - PHP中的随机 session 数据丢失

javascript - IONIC 框架(AngularJs 和 JavaScript)|是否推荐基于 SOAP 的 Web 服务?

c# - 如何在 web api 服务中发送两个或多个参数?