我在阅读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.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.
?>
这是回复
//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>
我正在尝试将 cis:shipmentNumber 和 labelUrl 加载到变量中,但由于此节点/子项,我无法正确引用它。有人能帮我一把吗?
谢谢!
答案 0 :(得分: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
答案 1 :(得分:-1)
您可以使用XPath:
/皂:信封/皂:车身/ BCS:CreateShipmentOrderResponse / CreationState / LabelData /顺式:shipmentNumber
我使用https://www.freeformatter.com/xpath-tester.html测试了XPath。
有关XPath示例,请参阅此答案:
Using xpath on a PHP SimpleXML object, SOAP + namespaces (not working..)