2017-09-14 7 views
1

SOAP応答の読み込みに問題があります。変数にshipmentNumberとlabelUrlが、私は:ここでは PHPのSOAP応答を読む

は、私はこれが応答

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

私はシスをロードしようとしている

<?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. 

?> 

を使用しているコードですこのノード/子のために正しく参照するのに問題があります。誰かが私に手を差し伸べることができる?

ありがとうございます!

+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

これがあなたの問題を解決した場合は、それを返答としてマークすることができます(答えの左側にある目盛り)。 –

-1

あなたは、XPathを使用することができます:私はhttps://www.freeformatter.com/xpath-tester.htmlを使用してXPathをテストしshipmentNumber

/石鹸:封筒/ソープ:ボディ/ BCS:CreateShipmentOrderResponse/CreationState/LabelData /シス。あなたはそれを使っている名前空間を登録する必要がXPathので、そのため異なる名前空間の

Using xpath on a PHP SimpleXML object, SOAP + namespaces (not working..)