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
            <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);


if($response->status == '0') {
echo "message successfully delivered to ".$mobileno. "<br>" ;
echo "error sending message to ".$mobileno."<br>" ;

The problem i have is being able to set or parse the XML response for to get the <status> XML tags. 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>0</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>

I would appreciate to get this solved

Neither what you are sending nor what you are getting are valid XMLs. Althowgh there is a probability that the request is meant to be XML like and not XML (maybe that is why you add XML= in front) the response should have been a valid XML. The question is , is what you are getting for one request or for three?

In the response you posted there are three different XMLs that have the same structure. If you got them all in one response you could split them and use SimpleXML parser. If it is the response of all requests and each one of them have each own XML than just use SimpleXML parser to take the status.

Member Avatar

Just wondering - the infobip allows json response as well as XML. JSON can be converted to PHP array easily with json_decode(), making life a lot easier. Just a thought.

The request (same message to multiple recipients) could be in this format:

$request = new HttpRequest();

  'accept' => 'application/json',
  'content-type' => 'application/json',
  'authorization' => 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=='

   "text":"Test SMS."

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;

The above code as taken from https://dev.infobip.com/docs/send-single-sms