dongyiyu882684 2014-12-04 10:17
浏览 141

如何为SOAP定义结构

I have an example in XML:

<?xml version="1.0" encoding="windows-1250"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ImportPackagesExt xmlns="PPLIEGate">
      <FirCode>ppltest</FirCode>
      <Packages>
        <Items>
          <anyType xsi:type="Package">
            <PackageID>40080145103</PackageID>
            <ZipCode>16300</ZipCode>
            <Country>CZ</Country>
            <ErrorCode>0</ErrorCode>
            <ErrorMessage></ErrorMessage>
          </anyType>
          <anyType xsi:type="Package">
            <PackageID>40010529207</PackageID>
            <ZipCode>15500</ZipCode>
            <Country>CZ</Country>
            <ErrorCode>0</ErrorCode>
            <ErrorMessage></ErrorMessage>
          </anyType>
        </Items>
      </Packages>
    </ImportPackagesExt>
  </soap:Body>
</soap:Envelope>

But this my PHP script returns an error.

$packs = [
    [
        'PackageID' => '40080145103',
        'ZipCode' => '16300',
        'Country' => 'CZ',
        'ErrorCode' => 0,
        'ErrorMessage' => '',
    ],
    [
        'PackageID' => '40010529207',
        'ZipCode' => '15500',
        'Country' => 'CZ',
        'ErrorCode' => 0,
        'ErrorMessage' => '',
    ],
];

$items = [];
foreach ($packs as $pack) {
    $items[] = new SoapVar($pack, SOAP_ENC_ARRAY, XSD_ANYTYPE, null, 'Package');
}

$packages = [
    'FirCode' => $firCode,
    'Packages' => [
        'Items' => $items,
        'ErrorCode' => 0,
    ],
    'DepID' => '00',
];
$response = $soap->ImportPackagesExt($packages);

Error says: Server was unable to process request. ---> Unable to cast object of type 'System.Xml.XmlNode[]' to type 'Package'.

I still can not figure out how to define a structure for the "Package".

  • 写回答

1条回答 默认 最新

  • dq804806 2014-12-05 08:18
    关注

    Got it. The variable $items must also pass as a instance of SoapVar.

    $parameters = [
        'FirCode' => $firCode,
        'Packages' => [
            'Items' => new SoapVar($items, SOAP_ENC_OBJECT),
            'ErrorCode' => 0,
            'ErrorMessage' => '',
        ],
        'DepID' => '00',
    ];
    
    评论

报告相同问题?