duan0504 2017-09-14 14:54
浏览 99
已采纳

在PHP中读取SOAP响应

I'm having a problem reading SOAP response, can anyone could help me and look ?

Here's the code I'm using

<?php 
//Data, connection, auth
$dataFromTheForm = $_POST['fieldName']; // request data from the form
$soapUrl = "https://cig.dhl.de/services/sandbox/soap"; // asmx URL of WSDL
$soapUser = "user";  //  username
$soapPassword = "password"; // password

//variables
//account details
$CisUser = "2222222222_01";
$CisPassword = "pass";
$Product = "V01PAK";
$EkpAccount = "22222222220101";
//shipment details
//$ShipmentDate = "2017-09-01"; //optional
$Reference = "ref1";
$Weight = "2.5";
//shipper details, same as return details
$ShipperName = "name";
$ShipperStreetName = "street";
$ShipperStreetNumber = "number";
$ShipperPostCode = "postcode";
$ShipperCity = "City";
$ShipperCountry = "Country";
$ShipperCountryISO = "DE";
//receiver details
$ReceiverName = "name";
$ReceiverStreetName = "street";
$ReceiverStreetNumber = "number";
$ReceiverPostCode = "postcode";
$ReceiverCity = "city";
$ReceiverCountry = "country";
$ReceiverCountryISO = "DE";
$RecipientEmail = "email.example@mail.com"; //optional, email notification


// xml post structure
$xml_post_string = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:cis='http://dhl.de/webservice/cisbase' xmlns:bus='http://dhl.de/webservices/businesscustomershipping'>
<soapenv:Header>
<cis:Authentification>
<cis:user>".$CisUser."</cis:user>
<cis:signature>".$CisPassword."</cis:signature>
</cis:Authentification>
</soapenv:Header>
<soapenv:Body>
<bus:CreateShipmentOrderRequest>
<bus:Version>
<majorRelease>2</majorRelease>
<minorRelease>0</minorRelease>
<!--Optional:-->
<build>?</build>
</bus:Version>
<!--1 to 30 repetitions:-->
<ShipmentOrder>
<sequenceNumber>01</sequenceNumber>
<Shipment>
<ShipmentDetails>
<product>".$Product."</product>
<cis:accountNumber>".$EkpAccount."</cis:accountNumber>
<customerReference>".$Reference."</customerReference>
<!--Optional <shipmentDate>".$ShipmentDate."</shipmentDate> -->
<!--Optional <returnShipmentAccountNumber></returnShipmentAccountNumber> -->
<returnShipmentReference>Retouren-Sendungsreferenz</returnShipmentReference>
<ShipmentItem>
<weightInKG>".$Weight."</weightInKG>
<!--Optional <lengthInCM>120</lengthInCM> -->
<!--Optional <widthInCM>60</widthInCM> -->
<!--Optional <heightInCM>60</heightInCM> -->
</ShipmentItem>
<!--Optional:-->
<Service>
<!-- You may enter the following 16 items in any order -->
<!-- <VisualCheckOfAge active='1' type='A16'/> -->
<!-- <PreferredLocation active='0' details='?'/> -->
<!-- <PreferredNeighbour active='0' details='?'/> -->
<!-- <GoGreen active='1'/> -->
<!-- <Personally active='0'/> -->
<!-- <CashOnDelivery active='1' codAmount='23.25'/> -->
<!-- <AdditionalInsurance active='1' insuranceAmount='2500'/> -->
<!-- <BulkyGoods active='1'/> -->
</Service>
<!--Optional:-->
<Notification>
<recipientEmailAddress>".$RecipientEmail."</recipientEmailAddress>
</Notification>
<!--Optional:-->
<BankData>
<!-- <cis:accountOwner>Max Mustermann</cis:accountOwner> -->
<!-- <cis:bankName>Postbank</cis:bankName> -->
<!-- <cis:iban>DE77100100100123456789</cis:iban> -->
<!-- <cis:note1>note 1</cis:note1> -->
<!-- <cis:note2>note 2</cis:note2> -->
<!-- <cis:bic>PBNKDEFFXXX</cis:bic> -->
<!-- <cis:accountreference>?</cis:accountreference> -->
</BankData>
</ShipmentDetails>
<Shipper>
<Name>
<cis:name1>".$ShipperName."</cis:name1>
<!--Optional:-->
<cis:name2/>
<!--Optional:-->
<cis:name3/>
</Name>
<Address>
<cis:streetName>".$ShipperStreetName."</cis:streetName>
<cis:streetNumber>".$ShipperStreetNumber."</cis:streetNumber>
<!--0 to 2 repetitions:-->
<cis:addressAddition>?</cis:addressAddition>
<!--Optional:-->
<cis:dispatchingInformation>?</cis:dispatchingInformation>
<cis:zip>".$ShipperPostCode."</cis:zip>
<cis:city>".$ShipperCity."</cis:city>
<!--Optional:-->
<cis:Origin>
<!--Optional:-->
<cis:country>".$ShipperCountry."</cis:country>
<!--Optional:-->
<cis:countryISOCode>".$ShipperCountryISO."</cis:countryISOCode>
<!--Optional:-->
<cis:state>?</cis:state>
</cis:Origin>
</Address>
<Communication>
<!--Optional:-->
<cis:phone/>
<!--Optional:-->
<cis:email/>
<!--Optional:-->
<cis:contactPerson/>
</Communication>
</Shipper>
<Receiver>
<cis:name1>".$ReceiverName."</cis:name1>
<!--You have a CHOICE of the next 4 items at this level-->
<Address>
<!--Optional:-->
<cis:name2/>
<!--Optional:-->
<cis:name3/>
<cis:streetName>".$ReceiverStreetName."</cis:streetName>
<cis:streetNumber>".$ReceiverStreetNumber."</cis:streetNumber>
<!--0 to 2 repetitions:-->
<cis:addressAddition>?</cis:addressAddition>
<!--Optional:-->
<cis:dispatchingInformation>?</cis:dispatchingInformation>
<cis:zip>".$ReceiverPostCode."</cis:zip>
<cis:city>".$ReceiverCity."</cis:city>
<!--Optional:-->
<cis:Origin>
<!--Optional:-->
<cis:country>".$ReceiverCountry."</cis:country>
<!--Optional:-->
<cis:countryISOCode>".$ReceiverCountryISO."</cis:countryISOCode>
<!--Optional:-->
<cis:state>?</cis:state>
</cis:Origin>
</Address>
<Communication>
<!--Optional:-->
<cis:phone/>
<!--Optional:-->
<cis:email/>
<!--Optional:-->
<cis:contactPerson/>
</Communication>
</Receiver>
<!--Optional:-->
<ReturnReceiver>
<Name>
<cis:name1>".$ShipperName."</cis:name1>
<!--Optional:-->
<cis:name2/>
<!--Optional:-->
<cis:name3/>
</Name>
<Address>
<cis:streetName>".$ShipperStreetName."</cis:streetName>
<cis:streetNumber>".$ShipperStreetNumber."</cis:streetNumber>
<!--0 to 2 repetitions:-->
<cis:addressAddition>?</cis:addressAddition>
<!--Optional:-->
<cis:dispatchingInformation>?</cis:dispatchingInformation>
<cis:zip>".$ShipperPostCode."</cis:zip>
<cis:city>".$ShipperCity."</cis:city>
<!--Optional:-->
<cis:Origin>
<!--Optional:-->
<cis:country>".$ShipperCountry."</cis:country>
<!--Optional:-->
<cis:countryISOCode>".$ShipperCountryISO."</cis:countryISOCode>
<!--Optional:-->
<cis:state>?</cis:state>
</cis:Origin>
</Address>
<Communication>
<!--Optional:-->
<cis:phone/>
<!--Optional:-->
<cis:email/>
<!--Optional:-->
<cis:contactPerson/>
</Communication>
</ReturnReceiver>
<!--Optional:-->
</Shipment>
<!--Optional:-->
<PrintOnlyIfCodeable active='1'/>
<!--Optional:-->
<labelResponseType>URL</labelResponseType>
</ShipmentOrder>
</bus:CreateShipmentOrderRequest>
</soapenv:Body>
</soapenv:Envelope>";

$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
//"SOAPAction: https://cig.dhl.de/services/sandbox/soap", 
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL

// PHP cURL  for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $soapUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// converting
$response = curl_exec($ch); 
curl_close($ch);


// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);

// convertingc to XML
$parser = simplexml_load_string($response2);
// user $parser to get your data out of XML response and to display it.

?> 

This is the response

//echo htmlentities($response);

soap:Envelope xmlns:bcs="http://dhl.de/webservices/businesscustomershipping" xmlns:cis="http://dhl.de/webservice/cisbase" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Header xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
<soap:Body>
      <bcs:CreateShipmentOrderResponse>
         <bcs:Version>
            <majorRelease xmlns="">2</majorRelease>
            <minorRelease xmlns="">0</minorRelease>
         </bcs:Version>
         <Status xmlns="">
            <statusCode>0</statusCode>
            <statusText>ok</statusText>
            <statusMessage>Der Webservice wurde ohne Fehler ausgeführt.</statusMessage>
         </Status>
         <CreationState xmlns="">
            <sequenceNumber>01</sequenceNumber>
            <LabelData>
               <Status>
                  <statusCode>0</statusCode>
                  <statusText>ok</statusText>
                  <statusMessage>Der Webservice wurde ohne Fehler ausgeführt.</statusMessage>
               </Status>
               <cis:shipmentNumber>222201010015736842</cis:shipmentNumber>
               <labelUrl>https://cig.dhl.de/gkvlabel/SANDBOX/dhl-vls/gw/shpmntws/printShipment?token=JD7HKktuvugIFEkhSvCfbEz4J8Ah0dkcVuw4PzBGRyTRdVHiR%2F6g1JB6pWINPQbTUI0kGODeACs1FyfHseJ6WhW9FK2aRyMHJfa6J%2Bwpy6Q%3D</labelUrl>
            </LabelData>
         </CreationState>
      </bcs:CreateShipmentOrderResponse>
   </soap:Body>
</soap:Envelope>

I'm trying to load cis:shipmentNumber and labelUrl into variables but I'm having problems referencing it properly because of this node/children. Could anyone give me hand?

Thanks!

  • 写回答

2条回答 默认 最新

  • dougaodi8895 2017-09-14 18:24
    关注

    Because of the different namespaces, with XPath you need to register the namespace it's using. The code below uses XPath to get the shipmentNumber, but directly accesses the labelUrl as it's not in another namespace.

    $xml=simplexml_load_string($response2);
    $xml->registerXPathNamespace("cis", "http://dhl.de/webservice/cisbase");
    
    $labelData = $xml->xpath("//LabelData")[0];
    echo "shipmentNumber=".$labelData->xpath("descendant::cis:shipmentNumber")[0].PHP_EOL;
    echo "labelUrl=".$labelData->labelUrl.PHP_EOL;
    

    If your using these values in anything other than in echo you may need to cast them to a string - (string)$labelData->labelUrl

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?