简体   繁体   English

基于条件语句格式化来自 php curl 的 XML 响应

[英]conditional statement based formatting of XML responses from php curl

I am having difficulty formatting the XML response from a php curl XML API for INFOBIP.我在格式化来自 INFOBIP 的 php curl XML API 的 XML 响应时遇到了困难。 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.但我想格式化 XML 响应以显示成功发送的消息以及使用条件 if else 语句未成功发送的消息。

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.我遇到的问题是能够根据 XML 状态响应设置或解析 XML 响应以获取<status>是否成功发送。 Currently this how the XML responses outputs the results without formatting.目前这是 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>
$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';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM