dtjbcda841554 2016-02-01 07:24 采纳率: 0%
浏览 45
已采纳

基于条件语句的PHP响应XML响应格式

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>
  • 写回答

1条回答 默认 最新

  • doushan1157 2016-02-01 12:34
    关注
    $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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里