2017-12-19 5 views
0

PHP curl을 사용하여 보내야하는 SOAP 요청의 값을 채우려고합니다. soap.xml 요청으로 변수를 설정하고 전달해야하는 모든 데이터를 가져 왔습니다.XML 변수에 자식 노드 추가하기 - PHP

$shipBody = '<?xml version="1.0" encoding="utf-8"?> 
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v1="http://servicescom/ws/merchantAPI/v1.0"> 
     <soap:Header> 
     <wsse:Security xmlns:wsse="http://docs.oasis-1.xsd"> 
      <wsse:UsernameToken> 
      <wsse:Username>'.$username.'</wsse:Username> 
      <wsse:Password>'.$password.'</wsse:Password> 
      </wsse:UsernameToken> 
     </wsse:Security> 
     </soap:Header> 
     <soap:Body> 
     <v1:parcelShipmentNotification> 
      <parcel> 
      <carrierName>'.$carrierName.'</carrierName> 
      <carrierService>'.$carrierService.'</carrierService> 
      '.$xml_data->children('itemdatastring')->asXML().'<!--Tried adding data as children but didn't return anything--> 
      '.$xml_data->asXML().'<!--This worked but i couldn't remove the extra "itemdatastring"--> 
      <orderId>'.$exId.'</orderId> 
      <parcelId>'.$parcelId.'</parcelId> 
      <parcelReference>'.$shipDate.'</parcelReference> 
      <shippingDate>'.$shipDate.'</shippingDate> 
      <trackingURL>https://tools.usps.com/go/TrackConfirmAction?tLabels='.$trackingUrl.'</trackingURL> 
      </parcel> 
      <requestPackingSlip>true</requestPackingSlip> 
     </v1:parcelShipmentNotification> 
     </soap:Body> 
    </soap:Envelope>'; 

나는 SKU와 수량 모든 개별 항목을 얻을 수있는 함수를 만들 수 있어요하지만 난 $ shipBody 변수에 반환 값을 전달하는 데 어려움입니다. 샘플 광고 항목은 다음과 같습니다.

$myShipment = array ( 
array(
    'sku' => 'BA2222222', 
    'qty' => '2' 
    ), 
array(
    'sku' => 'BA111111', 
    'qty' => '4' 
    ), 
); 

다음은 현재 광고 항목에 대한 XML을 얻는 방법입니다.

function array_to_xml($dataToConvert, $xml_data) { 
    foreach($dataToConvert as $key => $value) { 
     if(is_numeric($key)){ 
      $key = 'items'; //dealing with <0/>..<n/> issues 
     } 
     if(is_array($value)) { 
      $subnode = $xml_data->addChild($key); 
      array_to_xml($value, $subnode); 
     } else { 
      $xml_data->addChild("$key",htmlspecialchars("$value")); 
     } 
    } 
} 

$orderLineItems = array(); 
foreach ($myShipment as $orderItem) { 
    $orderItemSku = $orderItem['sku']; 
    $orderItemQty = $orderItem['qty']; 

    $orderLineItems[] = array(
     'quantity' => $orderItemQty, 
     'sku' => $orderItemSku 
    ); 
} 

$xml_data = new SimpleXMLElement('<itemdatastring></itemdatastring>'); 
array_to_xml($orderLineItems,$xml_data); 

마지막으로 완성 된 파일은 다음과 같습니다.

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:v1="http://servicescom/ws/merchantAPI/v1.0"> 
    <soap:Header> 
    <wsse:Security xmlns:wsse="http://docs.oasis-1.xsd"> 
     <wsse:UsernameToken> 
     <wsse:Username>'.$username.'</wsse:Username> 
     <wsse:Password>'.$password.'</wsse:Password> 
     </wsse:UsernameToken> 
    </wsse:Security> 
    </soap:Header> 
    <soap:Body> 
    <v1:parcelShipmentNotification> 
     <parcel> 
     <carrierName>'.$carrierName.'</carrierName> 
     <carrierService>'.$carrierService.'</carrierService> 
     <items> 
      <quantity>2</quantity> 
      <sku>BA2222222</sku> 
     </items> 
     <items> 
      <quantity>4</quantity> 
      <sku>BA111111</sku> 
     </items> 
     <orderId>'.$exId.'</orderId> 
     <parcelId>'.$parcelId.'</parcelId> 
     <parcelReference>'.$shipDate.'</parcelReference> 
     <shippingDate>'.$shipDate.'</shippingDate> 
     <trackingURL>https://tools.usps.com/go/TrackConfirmAction?tLabels='.$trackingUrl.'</trackingURL> 
     </parcel> 
     <requestPackingSlip>true</requestPackingSlip> 
    </v1:parcelShipmentNotification> 
    </soap:Body> 
</soap:Envelope> 

도움/의견을 보내 주시면 감사하겠습니다.

+0

약간의 예제를 단순화하기 위해 [편집] 할 수 있었습니까? 여기에 많은 여분의 XML이 있습니다. 실제로 문제와 관련이 없다고 생각합니다. 즉, ''및 ''이 (가) 많이 포함 된 XML 문서를 만드는 경우에도 [mcve]로 축소하십시오. – IMSoP

답변

0

->children()으로 전화하면 노드 목록이 다시 나타나므로 $xml_data->children('itemdatastring')->asXML()을 호출하면 실패합니다. 각각의 요소에서 문자열을 구축 한 다음 XML에 $itemString을 사용합니다 당신이 좋아하는 뭔가를 할 수

...

$itemString = ''; 
foreach ($xml_data->children() as $item) { 
    $itemString .= $item->asXML(); 
} 

.

+0

여기서 'itemdatastring'으로 전달한 인수는 네임 스페이스 URI로 해석됩니다. 여러분이 실제로 원했던 것은'foreach ($ xml_data-> itemdatastring from $ item)'일 뿐이라는' '이라는 자식 요소였습니다. – IMSoP

+0

OP의 코드를 복사하면서 실제로 읽는 데 깨어나지 않는 문제 : -/Corrected - thanks. –

+0

감사합니다. –