how to wrap xml with header - php

I'm trying to make a web-service request (WSDL).
$s = new soapclient("http://www.wb-service-address.com",array('wsdl'));
$HotelInfo = new stdClass;
<HotelInfo softwareID="123" SessionId="153">
<Hotel ID="103" />
</HotelInfo>
$HotelInfo->xmlRequest = $paramsStr;
$result = $s->__call("SubmitXmlString",array($HotelInfo));
$obj_pros = get_object_vars($result);
$hotel_full_xml = $obj_pros['SubmitXmlStringResult'];
$hotel_full_xml = simplexml_load_string($hotel_full_xml);
How the request will look like if i want to wrap the XML with a header?

Related

PHP SOAP: Passing the same parameters with the same name

I can't get this to work. I've read a bunch on it on the forum already but I just can't seem to find the solution to this.
I've created a SOAP call and it's working etc., but when I try to pass the same parameters multiple times it just overwrites itself which is logical.
The code has to be done with objects only so I've used stdClass()
Example of the code below:
$relationCreate = new stdClass();
$relationCreate->credentials = new stdClass();
$relationCreate->credentials->ApiKey = ''; //Removed for security reasons.
$relationCreate->credentials->DatabaseId = ''; //Removed for security reasons.;
$relationCreate->credentials->UserId = ''; //Removed for security reasons.;
$relationCreate->parentRelationId = $company;
$relationCreate->relationEntityTypeId = "84a15869-5b88-49df-ad47-7b6f9648ae07";
//surname
$relationCreate->relationFieldValues = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData->Id = "9d549512-dc8a-4774-84d1-27a349e8a8c7";
$relationCreate->relationFieldValues->PvFieldValueData->Value = $name;
// This one has to repeat which does not work. Which is logical
$relationCreate->relationFieldValues = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData = new stdClass();
$relationCreate->relationFieldValues->PvFieldValueData->Id = "9d549512-dc8a-4774-84d1-27a349e8a8c7";
$relationCreate->relationFieldValues->PvFieldValueData->Value = $name;
The soap should look as followed I tested this using SoapUI:
<api:fieldValues>
<!--Zero or more repetitions:-->
<api:PvFieldValueData>
<api:Id>c2fcb464-92e6-4227-8672-56f88e219279</api:Id>
<!--Optional:-->
<api:Value>Test</api:Value>
</api:PvFieldValueData>
</api:fieldValues>
<api:fieldValues>
<!--Zero or more repetitions:-->
<api:PvFieldValueData>
<api:Id>d900fe23-8549-451c-82f4-c5918cb3abbb</api:Id>
<!--Optional:-->
<api:Value>Test</api:Value>
</api:PvFieldValueData>
</api:fieldValues>
WSDL file for reference: https://api.perfectview.nl/V1/perfectview.asmx?WSDL
References:
PHP SoapClient - Multiple attributes with the same key
SoapClient: how to pass multiple elements with same name?
You can try to put the items into an array.
Example:
$relationCreate->relationFieldValues = [];
// repeat this in a foreach loop:
$item = new stdClass();
$item->PvFieldValueData = new stdClass();
$item->PvFieldValueData->Id = $uuid;
$item->PvFieldValueData->Value = $name;
// Add item to values
$relationCreate->relationFieldValues[] = $item;

How to add more than one Element in the same Attribute - XML (PHP Code)?

I have a PHP code that is running well, however not as expected.
F.Y.I: It´s running a function that is collecting submitted data from a form. (piece of code not included here, cause it´s OK).
I need to have 2 fields inside "Dados": name and email, however only email is being recorded.
What should I do? Any clue?
My actual PHP code:
function my_generate_xml($posted_data)
$domDocument = new DOMDocument('1.0', 'ISO-8859-1');
$domDocument->formatOutput = true;
// build maximizer xml file
$xml_root = $domDocument->createElement('moduledata');
$xmlEntity = $domDocument->createElement('entity');
$xmlEntityTN = $domDocument->createAttribute('tablename');
$xmlEntityTN->value = 'Ent';
$xmlEntityFN = $domDocument->createAttribute('formatname');
$xmlEntityFN->value = 'Curriculum';
$xmlEntity->appendChild($xmlEntityTN);
$xmlEntity->appendChild($xmlEntityFN);
$xmlDefine = $domDocument->createElement('define');
$xmlDefine->nodeValue = $posted_data['nome'];
$xmlDefineN = $domDocument->createAttribute('name');
$xmlDefineN->value = 'ParamNome';
$xmlDefine->appendChild($xmlDefineN);
$xmlEntity->appendChild($xmlDefine);
$xmlSiga = $domDocument->createElement('SigaFiles');
$xmlSigaDN = $domDocument->createAttribute('Text');
$xmlSigaDN->value = 'SQG';
$xmlSiga->appendChild($xmlSigaDN);
// create node for current dados
$xml_dados = $domDocument->createElement('Dados');
$domElement = $domDocument->createElement('attribute',$posted_data['nome']);
$domElement = $domDocument->createElement('attribute',$posted_data['email']);
$domAttribute = $domDocument->createAttribute('domainname');
// Value for the created attribute
$domAttribute->value = 'Nome';
$domAttribute->value = 'Email';
$domElement->appendChild($domAttribute);
$xml_dados->appendChild($domElement);
$xmlSiga->appendChild($xml_dados);
$xmlEntity->appendChild($xmlSiga);
$xml_root->appendChild($xmlEntity);
$domDocument->appendChild($xml_root);
Desired XML output format:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<moduledata>
<entity tablename="Ent" formatname="Curriculum">
<define name="ParamNome">Thomas Edison</define>
<SigaFiles Text="SQG">
<Dados>
<attribute domainname="Nome">Thomas Edison</attribute>
<attribute domainname="Email">thomas.edison#gmail.com</attribute>
</Dados>
</SigaFiles>
</entity>
</moduledata>
Actual XML output format:
<?xml version="1.0" encoding="ISO-8859-1"?>
<moduledata>
<entity tablename="Ent" formatname="Curriculum">
<define name="ParamNome">Thomas Edison</define>
<SigaFiles Text="SQG">
<Dados>
<attribute domainname="Email">thomas.edison#gmail.com</attribute>
</Dados>
</SigaFiles>
</entity>
</moduledata>
Actual XML output format: (UPDATED)
<?xml version="1.0" encoding="ISO-8859-1"?>
<moduledata>
<entity tablename="Ent" formatname="Curriculum">
<define name="Thomas Edison">nome</define>
<SigaFiles Text="SQG">
<Dados>
<attribute domainname="Nome">Thomas Edison</attribute>
<attribute domainname="Email">thomas.edison#gmail.com</attribute>
</Dados>
</SigaFiles>
</entity>
</moduledata>
PHP Code (Updated)
<?php
function my_generate_xml($posted_data)
{
$domDocument = new DOMDocument('1.0', 'ISO-8859-1');
$domDocument->formatOutput = true;
// build maximizer xml file
$xml_root = $domDocument->createElement('moduledata');
$xmlEntity = $domDocument->createElement('entity');
$xmlEntityTN = $domDocument->createAttribute('tablename');
$xmlEntityTN->value = 'Ent';
$xmlEntityFN = $domDocument->createAttribute('formatname');
$xmlEntityFN->value = 'Curriculum';
$xmlEntity->appendChild($xmlEntityTN);
$xmlEntity->appendChild($xmlEntityFN);
$xmlDefine = $domDocument->createElement('define');
$xmlDefine->nodeValue = $posted_data['nome'];
$xmlDefineN = $domDocument->createAttribute('name');
$xmlDefineN->value = 'ParamNome';
$xmlDefine->appendChild($xmlDefineN);
$xmlEntity->appendChild($xmlDefine);
$xmlSiga = $domDocument->createElement('SigaFiles');
$xmlSigaDN = $domDocument->createAttribute('Text');
$xmlSigaDN->value = 'SQG';
$xmlSiga->appendChild($xmlSigaDN);
// create node for current dados
$xml_dados = $xmlSiga->appendChild($domDocument->createElement('Dados'));
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['nome']));
$domElement->setAttribute('domainname', 'Nome');
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['email']));
$domElement->setAttribute('domainname', 'Email');
$domDocument->appendChild($domElement);
$xmlSiga->appendChild($domDocument);
$xmlEntity->appendChild($xmlSiga);
$xml_root->appendChild($xmlEntity);
$domDocument->appendChild($xml_root);
// save it as a file for further processing
$content = chunk_split(base64_encode($domDocument->saveXML()));
$uploads = wp_upload_dir();
$domDocument->save($uploads['basedir'].'/prorh/'.(int)microtime(true).'.xml');
}
?>
Consider reorganizing your routine and append elements after their creation and not save all appendChild calls at the end. Since you reuse the same variable names keep all components (element, attribute, values) together. Recall XML is a tree structure that grows from the root:
// build maximizer xml file
$domDocument = new DOMDocument('1.0', 'ISO-8859-1');
$domDocument->formatOutput = true;
// moduledata root element
$xml_root = $domDocument->createElement('moduledata');
$domDocument->appendChild($xml_root);
// entity element
$xmlEntity = $domDocument->createElement('entity');
$xml_root->appendChild($xmlEntity);
$xmlEntityTN = $domDocument->createAttribute('tablename');
$xmlEntityTN->value = 'Ent';
$xmlEntityFN = $domDocument->createAttribute('formatname');
$xmlEntityFN->value = 'Curriculum';
$xmlEntity->appendChild($xmlEntityTN);
$xmlEntity->appendChild($xmlEntityFN);
// define element
$xmlDefine = $domDocument->createElement('define');
$xmlEntity->appendChild($xmlDefine);
$xmlDefine->nodeValue = $posted_data['nome'];
$xmlDefineN = $domDocument->createAttribute('name');
$xmlDefineN->value = 'ParamNome';
$xmlDefine->appendChild($xmlDefineN);
// SigaFiles element
$xmlSiga = $domDocument->createElement('SigaFiles');
$xmlEntity->appendChild($xmlSiga);
$xmlSigaDN = $domDocument->createAttribute('Text');
$xmlSigaDN->value = 'SQG';
$xmlSiga->appendChild($xmlSigaDN);
// dados element
$xml_dados = $domDocument->createElement('Dados');
$xmlSiga->appendChild($xml_dados);
// attribute child nodes
$domElement = $domDocument->createElement('attribute', $posted_data['nome']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Nome';
$domElement->appendChild($domAttribute);
$xml_dados->appendChild($domElement);
$domElement = $domDocument->createElement('attribute', $posted_data['email']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Email';
$domElement->appendChild($domAttribute);
$xml_dados->appendChild($domElement);
// OUTPUT TREE TO STRING
header("Content-type: text/xml");
echo $domDocument->saveXML();
You override the $domElement variable without appending the node.
// create node for current dados
$xml_dados = $domDocument->createElement('Dados');
$domElement = $domDocument->createElement('attribute',$posted_data['nome']);
$domElement = $domDocument->createElement('attribute',$posted_data['email']);
$domAttribute = $domDocument->createAttribute('domainname');
I suggest nesting the create* calls inside appendChild() calls.
// create node for current dados
$xml_dados = $xmlSiga->appendChild($domDocument->createElement('Dados'));
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['nome']));
$domElement->setAttribute('domainname', 'Nome');
$domElement = $xml_dados->appendChild($domDocument->createElement('attribute'));
$domElement->appendChild($domDocument->createTextNode($posted_data['email']));
$domElement->setAttribute('domainname', 'Email');
You do not need to create attributes as nodes, DOMElement::setAttribute() works just fine for that. But you should create text content as nodes. The second argument of DOMDocument::createElement() is broken and escapes only some special characters.

Passing xml to url

Im new to xml and i just want to pass an object to url and parse it to array . dont know what to do.
Here's my code:
$xml = "<user><name>".$name."</name><balance>".$balance."</balance><address>".$address."</address><mobile>".$mobile."</mobile></user>";
$simple = new SimpleXMLElement($xml);
$simple2 = $simple->asXML();
$sample = simplexml_load_string($simple2);
$url = $GLOBALS['urlPath'].'webportal_sqlanywhere/xml.php?id='.$(my xml);
$path_file = file_get_contents($url);
echo $path_file;

formatted xml file using php

i am using following code to create xml file using php
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('alerts');
$root = $doc->appendChild($root);
$alert = $doc->createElement('alert');
$alert = $root->appendChild($alert);
$id = $doc->createElement('id');
$id_text = $doc->createTextNode($api_id);
$id->appendChild($id_text);
$alert->appendChild($id);
$msg_type = $doc->createElement('msg_type');
$msg_type_text = $doc->createTextNode(1);
$msg_type->appendChild($msg_type_text);
$alert->appendChild($msg_type);
$doc->save($filename);
it saves xml file well formatted like this
<?xml version="1.0"?>
<alerts>
<alert>
<id>22</id>
<msg_type>1</msg_type>
</alert>
</alerts>
but when i append tags in existing file with following code it will not formatted
$doc = new DOMDocument();
$doc->formatOutput = true;
$xml = file_get_contents($filename);
$doc->loadXML($xml);
$root = $doc->firstChild;
$alert = $doc->createElement('alert');
$alert = $root->appendChild($alert);
$id = $doc->createElement('id');
$id_text = $doc->createTextNode($api_id);
$id->appendChild($id_text);
$alert->appendChild($id);
$msg_type = $doc->createElement('msg_type');
$msg_type_text = $doc->createTextNode(1);
$msg_type->appendChild($msg_type_text);
$alert->appendChild($msg_type);
$doc->save($filename);
xml file format will be like this
<alerts>
<alert>
<id>3</id>
<msg_type>1</msg_type>
<msg>Api Name:Loop LM (H-PO) has low credit.</msg>
<url>common/api/view_sms_api_list.php</url>
<status>0</status>
<create_date>1387351877</create_date>
</alert>
<alert><id>6</id><msg_type>1</msg_type></alert><alert><id>14</id><msg_type>1</msg_type></alert><alert><id>24</id><msg_type>1</msg_type></alert></alerts>
it will continue in single line when i append a alert tag in this xml file. what is the problem?
thanks in advance.
Add $doc->preserveWhiteSpace = false; before load your doc $doc->loadXML($xml);

Using complex structure with SOAP

The problem is that I cannot succeed to send an array of class through SOAP to a specific web service that I cannot change his structure.
Here below you'll see the schema of the Soap function:
http://www.elyotech.com/share/yoni/forum/developpeznet-soap.jpg
Here you'll find the code I'm corresponding to it:
$s = new soapclient($URL_WEBSERVICE,array('wsdl'=>true,"trace"=>true));
$params = new stdClass();
$params->loginInfo = new stdClass();
$params->loginInfo->UserName = 'username';
$params->loginInfo->Password = 'password';
$params->loginInfo->LanguageCode = 'en';
$params->reservation = new stdClass();
$params->reservation->EmailRecipient2 = 'yonia#yopmail.com';
$params->reservation->EmailRecipient3 = 'yonia2#yopmail.com';
$params->reservation->FirstName = 'Yoni'
foreach($extras as $id=>$extra)
{
$params->Extras[$id]= new stdClass();
$params->Extras[$id]->SelectedExtra = new stdClass();
$params->Extras[$id]->ExtensionData = new stdClass();
$params->Extras[$id]->SelectedExtra->Amount = 1;
$params->Extras[$id]->SelectedExtra->ID = $extra;
}
Here is the SOAP request that is sent:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/">
<SOAP-ENV:Body>
<ns1:CreateReservation>
<ns1:loginInfo>
<ns1:UserName>monusername</ns1:UserName>
<ns1:Password>monpass</ns1:Password>
<ns1:LanguageCode>en</ns1:LanguageCode>
</ns1:loginInfo>
<ns1:reservation>
<ns1:EmailRecipient2>yonia#yopmail.com</ns1:EmailRecipient2>
<ns1:EmailRecipient3>yonia2#yopmail.com</ns1:EmailRecipient3>
<ns1:FirstName>Yoni</ns1:FirstName>
</ns1:reservation>
</ns1:CreateReservation>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In this Soap request, I cannot find the Extras array of objects.
Do you have any idea of how I should create my object in order to send it correctly?

Categories