I am having difficulty formatting the XML response from a php curl XML API for INFOBIP. My code below loops SMS to mobile numbers. But I want to format the XML responses to show the messages successfully sent and those that were not successful using a conditional if else statement.
while ($row = mysql_fetch_array($result))
{
// XML-formatted data
$xmlString='
<SMS>
<authentication>
<username>'.$user.'</username>
<password>'.$pass.'</password>
</authentication>
<message>
<sender>'.$sender.'</sender>
<text>'.$message.'</text>
<recipients>
<gsm>'.$mobileno.'</gsm>
//<gsm>'.$mobileno1.'</gsm>
</recipients>
</message>
</SMS>';
$fields = "XML=" . urlencode($xmlString);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($response);
// Successfully sent messages usually outputs <status>0<status> while unsuccessful outputs 1,2,3
if($xml->status == '0') {
echo "message successfully delivered to ".$mobileno. "<br>" ;
}else{
echo "error sending message to ".$mobileno."<br>" ;
}
}
The problem I have is being able to set or parse the XML responses to get the <status> if successfully sent or not based on the XML status response. Currently this how the XML responses outputs the results without formatting.
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result><status>1</status><messageid></messageid><destination>23421</destination></result> </results>
<?xml version="1.0" encoding="UTF-8"?> <results> <result><status>-13</status><messageid></messageid><destination>23412</destination></result> </results>
<?xml version="1.0" encoding="UTF-8"?> <results> <result><status>0</status><messageid></messageid><destination>23444</destination></result>
</results>
$str_soap_xml='
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<status>1</status>
<messageid></messageid>
<destination>23421</destination>
</result>
</results>
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<status>-13</status>
<messageid></messageid>
<destination>23412</destination>
</result>
</results>
<?xml version="1.0" encoding="UTF-8"?>
<results>
<result>
<status>0</status>
<messageid></messageid>
<destination>23444</destination>
</result>
</results>';
/*
manipulate the dodgy, invalid xml by firstly stripping out the XML Prologs
then give a new ROOT node ( as valid xml can only have one root node )
and then, to make sure, append a new XML Prolog
*/
$str_soap_xml='<?xml version="1.0" encoding="UTF-8"?><root>'.trim( str_replace( '<?xml version="1.0" encoding="UTF-8"?>', '', $str_soap_xml ) ).'</root>';
$total=0;
define('BR','<br />');
/* create the domdocument object & load the string */
$dom=new DOMDocument('1.0','utf-8');
$dom->loadXML( $str_soap_xml );
/* find all result nodes */
$col=$dom->getElementsByTagName('result');
/* iterate through each result and find it's children */
foreach( $col as $node ){
foreach( $node->childNodes as $child ){
echo $child->tagName.' '.$child->nodeValue.BR;
if( $child->tagName=='status' && $child->nodeValue==0 ) echo 'Bad foo!';
elseif( $child->tagName=='status' ) $total++;
}
}
$dom=$col=$node=$child=null;
echo 'total: '.$total.' add this to db';
Related
i am trying to parse the xml data from the result of my payment page, here is my code
$text = '<?xml version="1.0" encoding="utf-8"?>
<Exception xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Error xsi:type="BBSException">
<Message>Unable to auth</Message>
<Result>
<IssuerId>3</IssuerId>
<ResponseCode>98</ResponseCode>
<ResponseText>Transaction already processed</ResponseText>
<ResponseSource>Netaxept</ResponseSource>
<TransactionId>52ca86375c18468d8d7425c7a53459e1</TransactionId>
<ExecutionTime>2018-05-23T14:16:48.6230323+02:00</ExecutionTime>
<MerchantId>718374</MerchantId>
<ExtraInfoOut>2030010</ExtraInfoOut>
<MaskedPan />
<MessageId>ccf05c6e0bd84e77862431ac22140d7c</MessageId>
</Result>
</Error>
</Exception>';
$xml = simplexml_load_string($text);
echo "my message is".$xml->Error->Message."<br>";
echo "my response code is".$xml->Error->Result->ResponseCode."<br>";
This code works fine and return the result as
my message is Unable to auth
my response code is 98
but when i am trying to get this result from the payment result page like this
$text = file_get_contents("https://xxxx/xxxxx/xxxxxx");
$xml = simplexml_load_string($text);
echo "my message is".$xml->Error->Message."<br>";
echo "my response code is".$xml->Error->Result->ResponseCode."<br>";
but this code is not returning the value from the xml, when the i print the $text, it show the xml output
<?xml version="1.0" encoding="utf-8"?>
<Exception xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Error xsi:type="BBSException">
<Message>Unable to auth</Message>
<Result>
<IssuerId>3</IssuerId>
<ResponseCode>98</ResponseCode>
<ResponseText>Transaction already processed</ResponseText>
<ResponseSource>Netaxept</ResponseSource>
<TransactionId>52ca86375c18468d8d7425c7a53459e1</TransactionId>
<ExecutionTime>2018-05-23T14:16:48.6230323+02:00</ExecutionTime>
<MerchantId>718374</MerchantId>
<ExtraInfoOut>2030010</ExtraInfoOut>
<MaskedPan />
<MessageId>ccf05c6e0bd84e77862431ac22140d7c</MessageId>
</Result>
</Error>
</Exception>
can you any one help me with this
Thanks in advance
$sXML = file_get_contents('xxxxxx');
$oXML = new SimpleXMLElement($sXML);
echo $oXML->Error->Result->ResponseCode;
I am trying to parse a soap response with simplexml_load_string(). I have my soap client set with trace = 1 and exceptions = 0 and $client->__getLastResponse() gives me this result:
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector#prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I then read the results into $xml:
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
$xml .= urldecode($client->__getLastResponse());
An finally try to echo an element with no luck:
$xml1 = simplexml_load_string($xml,null,null,'http://schemas.xmlsoap.org/soap/envelope/',true);
echo "ELEMENT:" . $xml1->Envelope->Body->MDHeader->userid;
I believe it is the namespace SOAP-ENV that is giving me the issue but I don't know how to resolve it.
You can see my test page at: http://www.primevaluationservices.com/myriad/test.php
I think you don't need this line because it is already in the xml:
$xml = "<?xml version='1.0' encoding='UTF-8'?>";
To get the 'userid', I think you can use the SimpleXMLElement children method:
$source = <<<SOURCE
<?xml version='1.0' encoding='UTF-8'?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<MDHeader>
<userid>inspector#prime.com</userid>
<password>prime123456</password>
<batchid>1234</batchid></MDHeader>
<RECORDSET>
<ROW id='0'>
<INSPECTIONS>
<FOLDER_ID>835410936</FOLDER_ID>
<FOLDER_ID>835221706</FOLDER_ID>
<FOLDER_ID>835222299</FOLDER_ID>
</INSPECTIONS>
</ROW>
</RECORDSET>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOURCE;
$xml1 = simplexml_load_string($source);
echo $xml1->children('SOAP-ENV', true)->Body->children('')->MDHeader->userid->__toString();
Or the SimpleXMLElement xpath method:
$elements = $xml1->xpath('//SOAP-ENV:Envelope/SOAP-ENV:Body/MDHeader/userid');
$userid = $elements[0]->__toString();
echo '<br>';
echo $userid;
Will both result in:
inspector#prime.com
Demo
I am wondering why the below code doesn't show the value of processResponse tag while it shows the whole XML and the Body tag.
This is the XML which I am handling
$xml = '<?xml version="1.0"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>
<wsa:MessageID>urn:df1231asfer5e4564affds</wsa:MessageID>
<wsa:ReplyTo><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>
</env:Header>
<env:Body>
<processResponse xmlns="http://xmlns.oracle.com/EligibilityProcess/EligibilityProcess/EligibilityBPEL">
<generatedMessageRefNo>451</generatedMessageRefNo>
<providerRefNo>41</providerRefNo>
<tpaRequestId>4184612387</tpaRequestId>
<contractHolder>Rami Zbeeb</contractHolder>
<contractNo>81456954</contractNo>
<guarantorName>ANC</guarantorName>
<eligibilityStatus>Success</eligibilityStatus>
<eligibilityReason xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<messageOrNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<patientShare></patientShare>
<consentForm></consentForm>
<webServTechStatus>Success</webServTechStatus>
<replyCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<replyDescription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</processResponse></env:Body></env:Envelope>';
I am using the SimpleXML class:
$res = new SimpleXMLElement($xml);
When I am showing the body XML it works:
$str = $res->children('env',true)->Body->asXML();
echo "<pre>",htmlentities($str),"</pre>";
However when showing the processResponse XML or string it doesn't work:
$str = $res->children('env',true)->Body->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";
Kindly advice.
You can get the children of Body to get the processResponse:
$str = $res->children('env',true)->Body->children()->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";
I have a SOAP response that I want to save to an XML file. When the response is written to the file, the SOAP envelope is written with it, making the XML file useless due to the error:
XML declaration allowed only at the start of the document in ...
In this case, XML is being declared twice:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><ns1:NDFDgenResponse xmlns:ns1="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<dwmlOut xsi:type="xsd:string">
<?xml version="1.0"?>
...
Is there a good way to strip out this SOAP envelope and just save what's between it?
Here's how I'm writing the response to the file:
$toWrite = htmlspecialchars_decode($client->__getLastResponse());
$fp = fopen('weather.xml', 'w');
fwrite($fp, $toWrite);
fclose($fp);
The problem is the htmlspecialchars_decode(). The envelope document contains other XML documents as text nodes. If you decode the entities in the XML document you will destroy it. Never use htmlspecialchars_decode() on an XML document.
Load the (envelope) XML into a DOM and read the needed value from it.
$xml = <<<'XML'
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:NDFDgenResponse
xmlns:ns1="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<dwmlOut xsi:type="xsd:string">
<?xml version="1.0"?>
<weather>XML</weather>
</dwmlOut>
</ns1:NDFDgenResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xpath->registerNamespace('ndfd', 'http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl');
$innerXml = $xpath->evaluate(
'string(/soap:Envelope/soap:Body/ndfd:NDFDgenResponse/dwmlOut)'
);
echo $innerXml;
Output:
<?xml version="1.0"?>
<weather>XML</weather>
I trying to parse XML file but getting parsing error.
Code ::
$xmlUrl = 'products.xml';
$xmlStr = file_get_contents($xmlUrl);
$xmlObj = simplexml_load_string($xmlStr);
XML file ::
<?xml version="1.0" encoding="UTF-8"?>
<result>
<orderlist>
<order_no>123123</order_no>
<date></date>
<client_name>Knapp's Donut Shop</client_name>
<sector>54</sector>
</orderlist>
</result>
I am getting error because of this tag
<client_name>Knapp's Donut Shop</client_name>
The conversion to a SimpleXML Object and the output works, see code example below.
Check your "products.xml" file for the correct UTF-8 encoding type.
<?php
$xml = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<result>
<orderlist>
<order_no>123123</order_no>
<date></date>
<client_name>Knapp & Donut Shop</client_name>
<sector>54</sector>
</orderlist>
</result>
XML;
$xml = str_replace(array("&", "&"), array("&", "&"), $xml);
$xmlObj = simplexml_load_string($xml);
var_dump($xmlObj);
echo PHP_EOL . $xmlObj->orderlist->client_name;
// Result: Knapp & Donut Shop