2017-12-19 6 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 - ありがとう。 –

+0

ありがとう、これは働いた。 –

関連する問題