【问题标题】:conditional statement based formatting of XML responses from php curl来自 php curl 的基于条件语句的 XML 响应格式
【发布时间】:2016-05-09 13:50:57
【问题描述】:

我在格式化来自 INFOBIP 的 php curl XML API 的 XML 响应时遇到了困难。我下面的代码将 SMS 循环到手机号码。但我想格式化 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>" ;

  }

   }

我遇到的问题是能够根据 XML 状态响应设置或解析 XML 响应以获取 &lt;status&gt; 是否成功发送。目前这是 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>

【问题讨论】:

  • 因为你的xml无效,检查一下here
  • 在 xml 中有多个 prologs 无效 - 在尝试解析 xml 之前使用 str_replace 删除它们
  • @RamRaider 请给我看一个示例代码,使用 str_replace 在解析 XML 之前删除序言。谢谢

标签: php xml curl infobip infobip-api


【解决方案1】:
$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';

【讨论】:

  • 我通过用 $response 替换 $str_soap_xml 进行了尝试。由于我的 xml 结果是从 $response = curl_exec($ch);但除了实际未格式化的 XML 响应之外,我没有输出任何内容。
  • 您将原始数据打印到屏幕上我并不感到惊讶 - 您需要为 curl 请求提供返回传输选项。 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  • 它现在似乎正在输出格式更好的响应。我得到了状态 -13BRmessageid BRdestination 234343BRstatus -13BRmessageid BRdestination 234332BR 。所以我用 '
    ' 替换了 BR 以分成几行。我现在如何编写条件语句来检查状态值是否 == '0' 以便将消息标记为成功,否则标记为不成功。
  • BR 只是 html br 标签的常量(我忘了在答案中定义)
  • 我如何操作这部分 foreach( $col as $node ){ foreach( $node->childNodes as $child ){ echo $child->tagName.' '.$child->nodeValue.'
    ';如果 为 0 则输出成功,否则输出不成功。
猜你喜欢
  • 2012-02-05
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2012-11-28
  • 1970-01-01
  • 2018-06-03
  • 1970-01-01
相关资源
最近更新 更多