I am trying to send a PHP SOAP request that has multiple stdclass objects. I cannot figure out how to send so the xml is properly formatted. The xml structure should look like the following (from SOAPui).
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ass="http://assignment.soap.assignshare"
xmlns:xsd="http://request.assignment.soap.assignshare/xsd"
xmlns:xsd1="http://datamodel.assignment.soap.assignshare/xsd">
<soapenv:Header/>
<soapenv:Body>
<ass:SetStaffOnDutyStatus>
<ass:SasPubSetStaffOnDutyStatus>
<xsd:Source>VendorC</xsd:Source>
<xsd:Dest>?</xsd:Dest>
<xsd:PubEventMsgTag>1</xsd:PubEventMsgTag>
<xsd:EventSetStaffOnDutyStatus>
<xsd:Timestamp>Thurs,03 Nov 2016,20:20:00 GMT</xsd:Timestamp>
<xsd:EventAction>Set</xsd:EventAction>
<xsd:Staff>
<xsd1:StaffID>10</xsd1:StaffID>
<xsd1:Last>Doe</xsd1:Last>
<xsd1:First>John</xsd1:First>
<xsd1:Middle>?</xsd1:Middle>
<xsd1:Role>?</xsd1:Role>
</xsd:Staff>
<xsd:Team>
<xsd1:TeamID>319</xsd1:TeamID>
<xsd1:TeamName>5 East</xsd1:TeamName>
</xsd:Team>
</xsd:EventSetStaffOnDutyStatus>
</ass:SasPubSetStaffOnDutyStatus>
</ass:SetStaffOnDutyStatus>
</soapenv:Body>
</soapenv:Envelope>
My PHP file is
<?php
$a = new StdClass();
$a->SasPubSetStaffOnDutyStatus = new StdClass();
$a->SasPubSetStaffOnDutyStatus->Source = 'Demo';
$a->SasPubSetStaffOnDutyStatus->Dest = 'http://192.168.50.3:26001/SAIWebService';
$a->SasPubSetStaffOnDutyStatus->PubEventMsgTag = 1;
$a->SasPubSetStaffOnDutyStatus->EventSetStaffOnDutyStatus = new StdClass($b);
$b = new StdClass();
$b->EventSetStaffOnDutyStatus = new StdClass();
$b->EventSetStaffOnDutyStatus->Timestamp = 'Thurs,03 Nov 2016,20:20:00 GMT';
$b->EventSetStaffOnDutyStatus->EventAction = 'Set';
$c = new StdClass();
$c->Staff = new StdClass();
$c->Staff->StaffID = 10;
$c->Staff->Last = 'Harry';
$c->Staff->First = 'Potter';
$c->Staff->Middle = '?';
$c->Staff->Role = '?';
$d = new StdClass();
$d->Team = new StdClass();
$d->Team->TeamID = 319;
$d->Team->TeamName = '5 East';
$d = (object)array_merge ((array)$b, (array)$c, (array)$d);
$e = (object)array_merge((array)$a, (array)$d);
$wsdl = "http://192.xxx.xx.x:26001/SAIWebservice?singlewsdl";
$client = new SoapClient($wsdl, array(
"trace"=>1,
"exceptions"=>0));
$values = $client->SetStaffOnDutyStatus($e);
echo "REQUEST HEADERS:\n" . $client->__getLastRequestHeaders() . "\n";
$xml = $values->SasPubRespSetStaffOnDutyStatus;
print "<pre>\n";
print_r($xml);
print "</pre>";
The output of the PHP file does not include the values of objects $b, $c & $d. Appreciate any guidance on what I'm doing wrong to merge the objects. Output is below.
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://request.assignment.soap.assignshare/xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ns2="http://assignment.soap.assignshare">
<SOAP-ENV:Body>
<ns2:SetStaffOnDutyStatus>
<ns2:SasPubSetStaffOnDutyStatus>
<ns1:Source>Demo</ns1:Source>
<ns1:Dest>http://192.168.50.3:26001/SAIWebService</ns1:Dest>
<ns1:PubEventMsgTag>1</ns1:PubEventMsgTag>
<ns1:EventSetStaffOnDutyStatus>
<ns1:Timestamp xsi:nil="true"/>
<ns1:EventAction xsi:nil="true"/>
<ns1:Staff xsi:nil="true"/>
<ns1:Team xsi:nil="true"/>
</ns1:EventSetStaffOnDutyStatus>
</ns2:SasPubSetStaffOnDutyStatus>
</ns2:SetStaffOnDutyStatus>
</SOAP-ENV:Body></SOAP-ENV:Envelope>
Related
I'm trying to create xml file,
I have problem to add attributes to child object.
$this->myXML = new SimpleXMLElement('<start></start>');
$a[] = $this->myXML->addChild('parent');
$y = $a[0];
$y->addAttribute('type', 'documentary');
$y->addAttribute('type2', 'documentary');
$y[0] =11;
$a[1] = $a[0];
$s = $a[1]->children();
$s[-1]['sample'] = 123;
$s[1] = 1212;
output
<?xml version="1.0" standalone="yes"?>
<start>
<parent type="documentary" type2="documentary">11</parent>
<parent sample="123">1212</parent>
</start>
This is ok for one attribute
$s = $a[1]->children();
$s[-1]['sample'] = 123;
$s[-1]['sample2'] = 123;
$s[1] = 1212;
output
<?xml version="1.0" standalone="yes"?>
<start>
<parent type="documentary" type2="documentary">11</parent
<parent sample="123">1212</parent>
<parent sample2="123"/>
</start>
I try something like this:
$s = $a[1]->children();
$s->parent = 12;
$s->addAttribute('xy',123);
$s->addAttribute('x',1234);
$w = clone $s->parent;
unset($s->parent);
$s[1] = $w[0];
output
<?xml version="1.0" standalone="yes"?>
<start>
<parent type="documentary" type2="documentary">11</parent>
<parent>12</parent>
</start>
It is possible to to add attributes to child?
i'm writing down a code using a webservice that pretty much sends an address information and return me a Tracking code and shipment informations using a Brazilian courier company.
The code is:
$url = "http://vpsrv.visualset.com.br/?wsdl";
$client = new SoapClient($url);
$parametrosVipp = new stdClass();
$parametrosVipp->PostagemVipp = new stdClass();
//webservice access data
$parametrosVipp->PostagemVipp->PerfilVipp = new stdClass();
$parametrosVipp->PostagemVipp->PerfilVipp->Usuario = "webservice";
$parametrosVipp->PostagemVipp->PerfilVipp->Token = "testewebservice";
$parametrosVipp->PostagemVipp->PerfilVipp->IdPerfil = "605";
//webservice address data
$parametrosVipp->PostagemVipp->Destinatario = new stdClass();
$parametrosVipp->PostagemVipp->Destinatario->CnpjCpf = "33323884845";
$parametrosVipp->PostagemVipp->Destinatario->Nome = "Rafael Ferreira";
$parametrosVipp->PostagemVipp->Destinatario->Endereco = "Al Americana";
$parametrosVipp->PostagemVipp->Destinatario->Numero = "156";
$parametrosVipp->PostagemVipp->Destinatario->Complemento = "casa";
$parametrosVipp->PostagemVipp->Destinatario->Bairro = "Jd Paulista";
$parametrosVipp->PostagemVipp->Destinatario->Cidade = "Vinhedo";
$parametrosVipp->PostagemVipp->Destinatario->UF = "SP";
$parametrosVipp->PostagemVipp->Destinatario->Cep = "13280000";
//store order number
$parametrosVipp->PostagemVipp->NotasFiscais = new stdClass();
$parametrosVipp->PostagemVipp->NotasFiscais->NotaFiscal = new stdClass();
$parametrosVipp->PostagemVipp->NotasFiscais->NotaFiscal->NrNotaFiscal = "11223344";
//package info
$parametrosVipp->PostagemVipp->Volumes = new stdClass();
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto = new stdClass();
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->Peso = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->Altura = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->Largura = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->Comprimento = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->CodigoBarraVolume = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->CodigoBarraCliente = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->ObservacaoVisual = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->Conteudo = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->AdicionaisVolume = "?";
$parametrosVipp->PostagemVipp->Volumes->VolumeObjeto->Etiqueta = "?";
$obj = new ObjectAndXML();
$retorno = $client->PostarObjeto($parametrosVipp);
$recordsXML = $obj->objToXML($retorno);
var_dump($recordsXML);
preg_match('#<Postagem>(.*?)</Postagem>#', $recordsXML, $match);
print_r($match);
so when i call the ->PostarObjeto method it returns me the following XML:
<Postagem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="">
<ContratoEct>
<NrContrato>9912329784</NrContrato><CodigoAdministrativo>0013291092</CodigoAdministrativo><NrCartao>0067335344</NrCartao>
</ContratoEct>
<Destinatario>
<CnpjCpf>37323884845</CnpjCpf>
<IeRg/><Nome>Rafael Ferreira</Nome>
<Endereco>Al Americana</Endereco>
<Numero>156</Numero>
<Complemento>casa</Complemento>
<Bairro>Jd Paulista</Bairro>
<Cidade>Vinhedo</Cidade>
<UF>SP</UF>
<Cep>13280000</Cep>
<Telefone/><TelefoneSMS/><Email/>
</Destinatario>
<Servico>
<ServicoECT>41068</ServicoECT>
</Servico>
<NotasFiscais>
<NotaFiscal>
<DtNotaFiscal/><SerieNotaFiscal/><NrNotaFiscal>11223344</NrNotaFiscal><VlrTotalNota/>
</NotaFiscal></NotasFiscais>
<Volumes>
<VolumeObjeto>
<Peso>0</Peso>
<Cubagem>117</Cubagem>
<Altura>4</Altura>
<Largura>11</Largura>
<Comprimento>16</Comprimento>
<CodigoBarraVolume>?</CodigoBarraVolume>
<CodigoBarraCliente>?</CodigoBarraCliente>
<ObservacaoVisual>?</ObservacaoVisual>
<PosicaoVolume>1</PosicaoVolume>
<Conteudo>?</Conteudo>
<ValorDeclarado/>
<AdicionaisVolume>?</AdicionaisVolume><VlrACobrar/>
<Etiqueta>PZ000000822BR</Etiqueta>
<ValorTarifa>10.96</ValorTarifa
><ValorAdicionais>0.00</ValorAdicionais>
<ValorPostagem>10.96</ValorPostagem>
<StEntregaSabado>0</StEntregaSabado>
<StEntregaDomiciliar>1</StEntregaDomiciliar>
<DiasUteisPrazo>5</DiasUteisPrazo>
</VolumeObjeto>
</Volumes>
<ListaErros/>
<StatusPostagem>Valida</StatusPostagem>
</Postagem>
I need to get the value between the tag <Etiqueta>PZ000000822BR</Etiqueta>. I tried most of the solutions I found here, such as DOMElement, simplexml, REGEX and got no success. I'm probably doing something wrong, but I can't figure out what it is, since i don't have a lot of experience with webservices.
Thank you all and sorry for the poor english.
Assuming your XML as $data you can do the following:
$data = '<Postagem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="">
<ContratoEct>
<NrContrato>9912329784</NrContrato><CodigoAdministrativo>0013291092</CodigoAdministrativo><NrCartao>0067335344</NrCartao>
</ContratoEct>
<Destinatario>
<CnpjCpf>37323884845</CnpjCpf>
<IeRg/><Nome>Rafael Ferreira</Nome>
<Endereco>Al Americana</Endereco>
<Numero>156</Numero>
<Complemento>casa</Complemento>
<Bairro>Jd Paulista</Bairro>
<Cidade>Vinhedo</Cidade>
<UF>SP</UF>
<Cep>13280000</Cep>
<Telefone/><TelefoneSMS/><Email/>
</Destinatario>
<Servico>
<ServicoECT>41068</ServicoECT>
</Servico>
<NotasFiscais>
<NotaFiscal>
<DtNotaFiscal/><SerieNotaFiscal/><NrNotaFiscal>11223344</NrNotaFiscal><VlrTotalNota/>
</NotaFiscal></NotasFiscais>
<Volumes>
<VolumeObjeto>
<Peso>0</Peso>
<Cubagem>117</Cubagem>
<Altura>4</Altura>
<Largura>11</Largura>
<Comprimento>16</Comprimento>
<CodigoBarraVolume>?</CodigoBarraVolume>
<CodigoBarraCliente>?</CodigoBarraCliente>
<ObservacaoVisual>?</ObservacaoVisual>
<PosicaoVolume>1</PosicaoVolume>
<Conteudo>?</Conteudo>
<ValorDeclarado/>
<AdicionaisVolume>?</AdicionaisVolume><VlrACobrar/>
<Etiqueta>PZ000000822BR</Etiqueta>
<ValorTarifa>10.96</ValorTarifa
><ValorAdicionais>0.00</ValorAdicionais>
<ValorPostagem>10.96</ValorPostagem>
<StEntregaSabado>0</StEntregaSabado>
<StEntregaDomiciliar>1</StEntregaDomiciliar>
<DiasUteisPrazo>5</DiasUteisPrazo>
</VolumeObjeto>
</Volumes>
<ListaErros/>
<StatusPostagem>Valida</StatusPostagem>
</Postagem>';
# add XML declaration to have a well-formed XML string
$data = '<?xml version="1.0" encoding="UTF-8"?>' . $data;
# load it as a SimpleXMLObject
$xml_obj = simplexml_load_string($data);
# select the desired element
$etiqueta = $xml_obj->xpath('//Etiqueta');
# get the string value
$result = (string) $etiqueta[0];
$result will give you "PZ000000822BR".
<?xml version="1.0" encoding="UTF-8" ?>
<SMS>
<authentification>
<username>xxxx</username>
<password>xxxx</password>
</authentification>
<recipients>
<number>8309042932</number>
</recipients>
</SMS>
my number node has a dynamically generated numbers for different people, i want to load all the numbers, but am getting only the last number.
Code used to create the xml string:
<?xml version="1.0" encoding="UTF-8" ?>
$xmlstring =
"<SMS>
<authentification>
<username>xxxx</username>
<password>xxxx</password>
</authentification>
<recipients>";
foreach($gsmnumbers as $number) {
$number = explode(",", $number);
foreach($number as $num) {
$count = count($num);
for($i = 0; $i < $count; $i++) {
$xmlHalf = "<gsm>$num</gsm>";
}
}
}
$xmlSecondHalf = "</recipients> </SMS>";
Try this, all sorts of mistakes in your code.
Basically if you use the .= syntax, you can concatenate to the end of an existing string where just using = will replace the string with the new value.
Also the <?xml version="1.0" encoding="UTF-8" ?> need to be in the string to identify it as a valid XML string.
$xmlstring = '
<?xml version="1.0" encoding="UTF-8" ?>
<SMS>
<authentification>
<username>xxxx</username>
<password>xxxx</password>
</authentification>
<recipients>';
foreach($gsmnumbers as $number) {
$nums = explode(",", $number);
foreach($nums as $num) {
$xmlstring .= "<gsm>$num</gsm>";
}
}
$xmlstring .= "</recipients></SMS>";
Don't write XML as string, use a XML library for that. A library normally prevents you from shooting into your own foot when it comes to creating XML. It also makes your code more readable. Example:
// process and transform input data
$gsmnumbers = ['1234,5678,9012'];
$gsms = [];
foreach ($gsmnumbers as $number) {
$nums = explode(",", $number);
foreach ($nums as $num) {
$gsms[] = $num;
}
}
// create XML
$request = new SimpleXMLElement('<SMS/>');
$authentication = $request->addChild('authentification');
$authentication->username = 'XXXX';
$authentication->password = 'XXXX';
$recipients = $request->addChild('recipients');
foreach ($gsms as $gsm) {
$recipients->addChild('gsm', $gsm);
}
echo $request->asXML();
Example output:
<?xml version="1.0"?>
<SMS><authentification><username>XXXX</username><password>XXXX</password></authentification><recipients><gsm>1234</gsm><gsm>5678</gsm><gsm>9012</gsm></recipients></SMS>
I need help with this webservice, it is returning this stdClass Object ( [GETOfertasAereasResult] => )
I need to return an array with all the values.
<?php
try {
$wsdl_url = 'http://portaldoagente.com.br/wsonlinetravel/funcoes.asmx?WSDL';
$client = new SOAPClient($wsdl_url);
$params = array(
'sLojaChave' => "Y2Y4ZGRkOWU=",
);
$return = $client->GETOfertasAereas($params);
print_r($return);
} catch (Exception $e) {
echo "Exception occured: " . $e;
}
?>
Assuming your data is like the following:
$return = '<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org /2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GETOfertasAereasResponse xmlns="http://tempuri.org/">
<GETOfertasAereasResult>Some result content</GETOfertasAereasResult>
</GETOfertasAereasResponse>
</soap12:Body>
</soap12:Envelope>';
Try this:
$obj = new SimpleXMLElement($return);
$body = $obj->children('soap12', true)->Body->children();
$content = (string) $body->GETOfertasAereasResponse->GETOfertasAereasResult;
var_dump($content);
I am trying to write a php script which will insert an object into an array of objects which is originally in XML format. I need to insert the object at a specified index and then be able to re-write the xml file from which the data was pulled with the updated object. Here is the structure of my XML
<?xml version="1.0" encoding="UTF-8"?>
<Bars>
<Bar>
<BarName>Kam's</BarName>
<bar_id>0</bar_id>
<Bartenders>
<Bartender>
<fname>Max</fname>
<lname>Vest</lname>
<imageURL>http://uofi-bars.com/bartenderImages/maxvest.jpg</imageURL>
<shift>2</shift>
</Bartender>
</Bartenders>
<Events>
<Event>
<EventName>Kams event</EventName>
<date>08/10/1989</date>
</Event>
</Events>
<Specials>
<Special>Kam's Special 1</Special>
<Special>Kam's Special 2</Special>
</Specials>
</Bar>
So in other words, if I have a bartender who works at a bar with an id of bar_id = 0, I need to be able to insert that bartender into the array of bartenders for that particular bar.
I use the following php code to create the arrays from XML:
function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
$arrData = array();
// if input is object, convert into array
if (is_object($arrObjData)) {
$arrObjData = get_object_vars($arrObjData);
}
if (is_array($arrObjData)) {
foreach ($arrObjData as $index => $value) {
if (is_object($value) || is_array($value)) {
$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
}
if (in_array($index, $arrSkipIndices)) {
continue;
}
$arrData[$index] = $value;
}
}
return $arrData;
}
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
I guess I just don't know how to refer to this array of objects within an array of objects... Any help would be greatly appreciated!
Thanks!
if you just replace your code:
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
$arrXml = objectsIntoArray($xmlObj);
print_r($arrXml);
with this:
$xmlUrl = "Bars.xml"; // XML
$xmlStr = file_get_contents($xmlUrl);
$xml = new SimpleXMLElement($xmlStr);
$bartenders = $xml->xpath('//Bartenders');
$new_bartender = $bartenders[0]->addChild('Bartender');
$new_bartender->fname = 'test1';
$new_bartender->lname = 'test2';
$new_bartender->imgURL = 'http://test.com';
$new_bartender->shift = '0';
print_r($bartenders);
this should do the trick, just replace the values with appropriate values :) i hope this helps!!