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

时间:2013-08-10 00:57:41

标签: 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" );

2 个答案:

答案 0 :(得分:0)

在处理.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);

没有问题。

答案 1 :(得分:0)

将XML作为字符串传递给XSD_ANYXML,因为类型就是答案。我还需要省略SoapVar()函数调用中的第三个和第四个参数。

$XML =  '<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="foo">
                                    <ota:CompanyName CompanyShortName="bat"/>
                            </ota:RequestorID>
                    </ota:Source>
            </POS>
            <TrainStatusInfo TrainNumber="'.$TrainNumber.'">
                    <TravelDate>
                            <ota:DepartureDateTime>'.$Today.'</ota:DepartureDateTime>
                    </TravelDate>

            </TrainStatusInfo>
        </TrainStatusRQ>';

$TrainStatusRQ = new SoapVar($XML,XSD_ANYXML);