我很难格式化来自 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>" ;
}
}
我遇到的问题是能够<status>
根据 XML 状态响应设置或解析 XML 响应以获取是否成功发送。目前,这是 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>