在PHP中读取SOAP响应

在PHP中读取SOAP响应,php,xml,soap,Php,Xml,Soap,我在阅读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

我在阅读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.

?> 
您可以使用XPath:

/soap:Envelope/soap:Body/bcs:CreateShipmenterResponse/CreationState/LabelData/cis:shipmentNumber

我使用测试工具测试了XPath

有关XPath示例,请参见以下答案:


由于名称空间不同,使用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

OP,为什么不使用
new SoapClient()
?,无论如何,请尝试
$parser->shipmentNumber如果这解决了您的问题,您可以将其标记为已回答(答案左侧的勾号)谢谢。
$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;