2017-09-14 130 views
1

我在阅读SOAP响应时遇到问题,任何人都可以帮助我看看?在PHP中读取SOAP响应

下面是我使用

<?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 protected]"; //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>DE7710010010</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. 

?> 

这是响应

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

我想顺加载代码:shipmentNumberlabelUrl到变量,但我由于这个节点/孩子而有问题引用它的问题。任何人都可以给我手?

谢谢!

+0

OP,你为什么不使用'新SoapClient的()'?反正试试'$ parser-> shipmentNumber;' – Kisaragi

回答

0

因为不同的命名空间中,使用XPath您需要注册它使用的命名空间。下面的代码使用XPath获取shipmentNumber,但直接访问labelUrl,因为它不在另一个名称空间中。

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

如果您在比echo之外的任何使用这些值,您可能需要将它们转换成字符串 - (string)$labelData->labelUrl

+0

感谢你的例子和你的帮助! – Pawel

+0

如果这解决了您的问题,您能否将其标记为已回答(答案左侧的勾号),谢谢。 –