Skip to content

Instantly share code, notes, and snippets.

@Guley
Created November 4, 2019 04:36
Show Gist options
  • Save Guley/a5c4b21003ce4da7d194df6f5d69b899 to your computer and use it in GitHub Desktop.
Save Guley/a5c4b21003ce4da7d194df6f5d69b899 to your computer and use it in GitHub Desktop.
PHP Genrate XML Envelope
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'on');
$xml = new DOMDocument('1.0', 'utf8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$startTag1 = $xml->createElement('startTag1');
function add_dom_child (&$parent, $tag, $value, $type = 'text') {
global $xml;
$node = $xml->createElement($tag);
$node = $parent->appendChild($node);
$value = trim(preg_replace('/\s+/', ' ',$value));
switch($type) {
case 'cdata':
$contxt = $xml->createCDATASection($value);
break;
case 'bool':
$value = ($value === true) ? 'TRUE' : 'FALSE';
$contxt = $xml->createTextNode($value);
break;
default:
$contxt = $xml->createTextNode($value);
break;
}
$contxt = $node->appendChild($contxt);
}
add_dom_child($startTag1, 'Field 1', 'value');
add_dom_child($startTag1, 'Field 2', 'value');
add_dom_child($startTag1, 'Field 3', 'value');
$xml->appendChild($startTag1);
/*Add Child Node*/
$startTag2 = $xml->createElement('startTag2');
$startTag2NodeName = $xml->createElement('startTag2NodeName');
add_dom_child($startTag2NodeName, 'DueDate', 'value');
add_dom_child($startTag2NodeName, 'ProductCode', 'value');
add_dom_child($startTag2NodeName, 'Description', 'value');
$startTag2->appendChild($startTag2NodeName);
$xml->appendChild($startTag2);
$xmlString = $xml->saveXML();
echo $xmlString;
/*Create Object to send*/
$data_obj = new SoapVar($xmlString, XSD_ANYXML);));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment