Question

I'm trying to pass a fairly complex parameter string which I have an XML example of and I'm trying to encode it properly using PHP. The example request I was given is this:

<?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>

And I'm going to call it use it like this

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; 
    }

Its the encoding of $TrainStatusRQ that I cant seem to figure out since there are attributes and multilevel parameters. This is as close as I have gotten.

$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" );
Was it helpful?

Solution

I had similar problems when dealing with a .NET service.

What I ended up with was assembling the structure as plain string.

 $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);

Worked without problems.

OTHER TIPS

Passing in the XML as a string with XSD_ANYXML as the type was the answer. I also needed to leave out the third and fourth parameters in the SoapVar() function call.

$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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top